JavaScript 只倒数小时、分钟和秒的时钟..不是天数
JavaScript clock that countsdown hours, mins and seconds only.. not days
我正在尝试制作一个 JavaScript 时钟,可以按小时而不是天进行倒计时。我使用了一个不错的 countdown JS script from Whosebug,我正在尝试根据自己的喜好调整代码。
我的代码:
// CLOCK
var end = new Date(2017,4,22,23,59);
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;
function clock() {
var now = new Date();
var distance = end - now;
var days = Math.floor(distance / _day);
// get the total day number example, 3 days
// times that by 3, so 3x24 = 72 hours
var plus = days * 24;
// no make the hours + the days, so 4 hours + 72 etc
var hours = Math.floor((distance % _day) / _hour + plus);
var minutes = Math.floor((distance % _hour) / _minute);
var seconds = Math.floor((distance % _minute) / _second);
console.log('Quick! Get the sale while it lasts')
console.log(+hours + ' hours remaining..');
console.log(+seconds + ' seconds remaining..');
console.log(+minutes + ' mins remaining..');
}
var countdownTimer = setInterval('clock()', 1000);
如果今天是 5 月 18 日,结束日期是 20 日,那么倒计时应该在 48 小时左右,但它无法正常工作。似乎在使用旧的 2 天 15 小时。我不想与天有任何关系。我想要小时数而不是天数。
有什么指点吗?
删除与天数相关的任何内容(变量、计算):
var end = new Date('5/22/2017 11:59 PM');
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
function clock(){
var now = new Date();
var distance = end - now;
var hours = Math.floor(distance / _hour);
var minutes = Math.floor((distance % _hour) / _minute);
var seconds = Math.floor((distance % _minute) / _second);
console.log('Quick! Get the sale while it lasts')
console.log(hours + ' hours remaining..');
console.log(minutes + ' mins remaining..');
console.log(seconds + ' seconds remaining..');
}
var countdownTimer = setInterval(clock, 1000);
注意:将字符串作为第一个参数传递给 setInterval
被认为是不好的做法;只需传递函数引用即可。
您会注意到,有时,计数器可能会跳一秒或两次显示相同的秒数,因为 setInterval
不能保证调用将 准确 由给定的延迟分隔。您可以通过计算下一秒何时到期来抵消这种影响,并将该动态延迟提供给 setTimeout
。这里还有一些代码可以在目标时刻到达后停止重复:
// CLOCK
var end = new Date('5/22/2017 11:59 PM');
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
function clock(){
var now = new Date();
var distance = end - now;
if (distance <= 0) {
console.log('Sorry, sale has ended.')
return; // stop repeating
}
// More precise delay, so you get a tick every second:
setTimeout(clock, _second*1.5 - now % _second);
var hours = Math.floor(distance / _hour);
var minutes = Math.floor((distance % _hour) / _minute);
var seconds = Math.floor((distance % _minute) / _second);
console.log(hours +':' + ('0' + minutes).substr(-2)
+ ':' + ('0' + seconds).substr(-2) + ' remaining...');
}
clock(); // Just call it
你可以简单地做一个函数:
function timestamp() {
let time = new Date();
document.getElementById('').innerHTML = time.toLocaleTimeString();
}
setInterval(timestamp, 1000);
在您的 getElementById() 中您的 ID 名称
我正在尝试制作一个 JavaScript 时钟,可以按小时而不是天进行倒计时。我使用了一个不错的 countdown JS script from Whosebug,我正在尝试根据自己的喜好调整代码。
我的代码:
// CLOCK
var end = new Date(2017,4,22,23,59);
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;
function clock() {
var now = new Date();
var distance = end - now;
var days = Math.floor(distance / _day);
// get the total day number example, 3 days
// times that by 3, so 3x24 = 72 hours
var plus = days * 24;
// no make the hours + the days, so 4 hours + 72 etc
var hours = Math.floor((distance % _day) / _hour + plus);
var minutes = Math.floor((distance % _hour) / _minute);
var seconds = Math.floor((distance % _minute) / _second);
console.log('Quick! Get the sale while it lasts')
console.log(+hours + ' hours remaining..');
console.log(+seconds + ' seconds remaining..');
console.log(+minutes + ' mins remaining..');
}
var countdownTimer = setInterval('clock()', 1000);
如果今天是 5 月 18 日,结束日期是 20 日,那么倒计时应该在 48 小时左右,但它无法正常工作。似乎在使用旧的 2 天 15 小时。我不想与天有任何关系。我想要小时数而不是天数。
有什么指点吗?
删除与天数相关的任何内容(变量、计算):
var end = new Date('5/22/2017 11:59 PM');
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
function clock(){
var now = new Date();
var distance = end - now;
var hours = Math.floor(distance / _hour);
var minutes = Math.floor((distance % _hour) / _minute);
var seconds = Math.floor((distance % _minute) / _second);
console.log('Quick! Get the sale while it lasts')
console.log(hours + ' hours remaining..');
console.log(minutes + ' mins remaining..');
console.log(seconds + ' seconds remaining..');
}
var countdownTimer = setInterval(clock, 1000);
注意:将字符串作为第一个参数传递给 setInterval
被认为是不好的做法;只需传递函数引用即可。
您会注意到,有时,计数器可能会跳一秒或两次显示相同的秒数,因为 setInterval
不能保证调用将 准确 由给定的延迟分隔。您可以通过计算下一秒何时到期来抵消这种影响,并将该动态延迟提供给 setTimeout
。这里还有一些代码可以在目标时刻到达后停止重复:
// CLOCK
var end = new Date('5/22/2017 11:59 PM');
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
function clock(){
var now = new Date();
var distance = end - now;
if (distance <= 0) {
console.log('Sorry, sale has ended.')
return; // stop repeating
}
// More precise delay, so you get a tick every second:
setTimeout(clock, _second*1.5 - now % _second);
var hours = Math.floor(distance / _hour);
var minutes = Math.floor((distance % _hour) / _minute);
var seconds = Math.floor((distance % _minute) / _second);
console.log(hours +':' + ('0' + minutes).substr(-2)
+ ':' + ('0' + seconds).substr(-2) + ' remaining...');
}
clock(); // Just call it
你可以简单地做一个函数:
function timestamp() {
let time = new Date();
document.getElementById('').innerHTML = time.toLocaleTimeString();
}
setInterval(timestamp, 1000);
在您的 getElementById() 中您的 ID 名称