总是有两个数字的倒计时,例如:02:20:02
Countdown alway with two numbers eg: 02:20:02
您好,我对这段代码有疑问。我尝试了几种方法,但没有成功在数小时前归零等。我还检查了不同的主题,但没有成功。
var timestamp = (Date.now() + 1000 * 2 * 60 * 24 * 1) - Date.now();
timestamp /= 1000;
function component(x, v) {
return Math.floor(x / v);
}
/* last thing i tried but maybe it will help someone
Number.prototype.pad = function(size) {
var s = String(this);
while (s.length < (size || 2)) {s = "0" + s;}
return s;
};
*/
var $div = $('div');
setInterval(function () {
timestamp--;
var
hours = component(timestamp, 60 * 60),
minutes = component(timestamp, 60) % 60,
seconds = component(timestamp, 1) % 60;
$div.html(hours + ":" + minutes + ":" + seconds);
}, 1000);
演示:http://jsfiddle.net/5z7ahmze/1/
感谢您的宝贵时间。
您可以检查您的变量并在需要时在前面添加一个 0 :
var comp = component(timestamp, 60 * 60);
var hour = comp < 10 ? '0' + comp : comp;
也许这就是您想要的。对吗?
编辑
好的,最后的答案。
var interval = setInterval(function () {
timestamp--;
function addZero (number) {
var zeroedNumber = (number < 10) ? 0 + "" + number : number;
return zeroedNumber;
}
var
hours = addZero(component(timestamp, 60 * 60)),
minutes = addZero(component(timestamp, 60) % 60),
seconds = addZero(component(timestamp, 1) % 60);
$div.html(hours + ":" + minutes + ":" + seconds);
//Below, i helped you with a "stop count" handler. (:
if(hours == 0 & minutes == 0 & seconds == 1){
clearInterval(interval);
}
}, 1000);
如果(小时或分钟或秒)小于 10,则动态地将零添加到您的计数器。
您可以像这样创建一个函数
function pad(number, length) {
var str = '' + number;
while (str.length < length) {
str = '0' + str;
}
return str;
}
然后
$div.html(pad(hours, 2) + ":" + pad(minutes, 2) + ":" + pad(seconds, 2));
我认为您的代码可以正常工作,如果您对数字调用 pad 函数:
$div.html(hours.pad() + ":" + minutes.pad() + ":" + seconds.pad());
您好,我对这段代码有疑问。我尝试了几种方法,但没有成功在数小时前归零等。我还检查了不同的主题,但没有成功。
var timestamp = (Date.now() + 1000 * 2 * 60 * 24 * 1) - Date.now();
timestamp /= 1000;
function component(x, v) {
return Math.floor(x / v);
}
/* last thing i tried but maybe it will help someone
Number.prototype.pad = function(size) {
var s = String(this);
while (s.length < (size || 2)) {s = "0" + s;}
return s;
};
*/
var $div = $('div');
setInterval(function () {
timestamp--;
var
hours = component(timestamp, 60 * 60),
minutes = component(timestamp, 60) % 60,
seconds = component(timestamp, 1) % 60;
$div.html(hours + ":" + minutes + ":" + seconds);
}, 1000);
演示:http://jsfiddle.net/5z7ahmze/1/
感谢您的宝贵时间。
您可以检查您的变量并在需要时在前面添加一个 0 :
var comp = component(timestamp, 60 * 60);
var hour = comp < 10 ? '0' + comp : comp;
也许这就是您想要的。对吗?
编辑
好的,最后的答案。
var interval = setInterval(function () {
timestamp--;
function addZero (number) {
var zeroedNumber = (number < 10) ? 0 + "" + number : number;
return zeroedNumber;
}
var
hours = addZero(component(timestamp, 60 * 60)),
minutes = addZero(component(timestamp, 60) % 60),
seconds = addZero(component(timestamp, 1) % 60);
$div.html(hours + ":" + minutes + ":" + seconds);
//Below, i helped you with a "stop count" handler. (:
if(hours == 0 & minutes == 0 & seconds == 1){
clearInterval(interval);
}
}, 1000);
如果(小时或分钟或秒)小于 10,则动态地将零添加到您的计数器。
您可以像这样创建一个函数
function pad(number, length) {
var str = '' + number;
while (str.length < length) {
str = '0' + str;
}
return str;
}
然后
$div.html(pad(hours, 2) + ":" + pad(minutes, 2) + ":" + pad(seconds, 2));
我认为您的代码可以正常工作,如果您对数字调用 pad 函数:
$div.html(hours.pad() + ":" + minutes.pad() + ":" + seconds.pad());