将 0 添加到此倒数计时器
Add 0's to this countdown timer
我终于成功地制作了我的 24 小时非日期可靠倒数计时器。这样做的目的是每次有人访问该站点时都会开始倒计时。问题是,当任何单位(小时、分钟、秒)达到个位数时,值将按原样显示,而不是标准时间格式(9 分钟而不是 09 分钟,它应该如此)。我如何实现一个条件,如果一个值 <= 9 它在它之前添加一个 0?
var count = 86400;
var counter = setInterval(timer, 1000); //1000 will run it every 1 second
function timer() {
count = count - 1;
if (count == -1) {
clearInterval(counter);
return;
}
var seconds = count % 60;
var minutes = Math.floor(count / 60);
var hours = Math.floor(minutes / 60);
minutes %= 60;
hours %= 60;
document.getElementById("timer").innerHTML = hours + ":" + minutes + ":" + seconds; // watch for spelling
}
<span id='timer'></span>
尝试...
document.getElementById("timer").innerHTML
= (hours<10 ? "0" + hours : hours) + ":"
+ (minutes<10 ? "0" + minutes : minutes) + ":"
+ (seconds<10 ? "0" + seconds : seconds);
基本上,如果值小于 10,则放置一个 "0"
;否则只是价值。另一种说法是 if condition ?然后:否则...
替代路线……更多代码为:
var t_hours, t_minutes, t_seconds;
if (hours<10) {
t_hours = "0" + hours;
} else {
t_hours = hours;
}
if (minutes<10) {
t_minutes = "0" + minutes;
} else {
t_minutes = minutes;
}
if (seconds<10) {
t_seconds = "0" + seconds;
} else {
t_seconds = seconds;
}
document.getElementById("timer").innerHTML
= t_hours + ":" t_minutes + ":" t_seconds;
您可以使用How to output integers with leading zeros in JavaScript
的通用键盘功能
function pad(num, size) {
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
}
并将您的代码更改为:
document.getElementById("timer").innerHTML = pad(hours,2) + ":" + pad(minutes,2) + ":" + pad(seconds,2);
创建一个类似于以下的函数来为您完成这项工作:
function makeMeTwoDigits(n){
return (n < 10 ? "0" : "") + n;
}
在打印您的号码之前调用此函数:
document.getElementById("timer").innerHTML = makeMeTwoDigits(hours) + ":" + makeMeTwoDigits(minutes) + ":" + makeMeTwoDigits(seconds);
解释:
就像@rfornal 说的那样,我们正在检查数字是否小于 10,这意味着单个数字,添加“0”和 return 否则不添加任何内容并且 return.
需要注意的一点是,如果数字为负数,这将不起作用。
我终于成功地制作了我的 24 小时非日期可靠倒数计时器。这样做的目的是每次有人访问该站点时都会开始倒计时。问题是,当任何单位(小时、分钟、秒)达到个位数时,值将按原样显示,而不是标准时间格式(9 分钟而不是 09 分钟,它应该如此)。我如何实现一个条件,如果一个值 <= 9 它在它之前添加一个 0?
var count = 86400;
var counter = setInterval(timer, 1000); //1000 will run it every 1 second
function timer() {
count = count - 1;
if (count == -1) {
clearInterval(counter);
return;
}
var seconds = count % 60;
var minutes = Math.floor(count / 60);
var hours = Math.floor(minutes / 60);
minutes %= 60;
hours %= 60;
document.getElementById("timer").innerHTML = hours + ":" + minutes + ":" + seconds; // watch for spelling
}
<span id='timer'></span>
尝试...
document.getElementById("timer").innerHTML
= (hours<10 ? "0" + hours : hours) + ":"
+ (minutes<10 ? "0" + minutes : minutes) + ":"
+ (seconds<10 ? "0" + seconds : seconds);
基本上,如果值小于 10,则放置一个 "0"
;否则只是价值。另一种说法是 if condition ?然后:否则...
替代路线……更多代码为:
var t_hours, t_minutes, t_seconds;
if (hours<10) {
t_hours = "0" + hours;
} else {
t_hours = hours;
}
if (minutes<10) {
t_minutes = "0" + minutes;
} else {
t_minutes = minutes;
}
if (seconds<10) {
t_seconds = "0" + seconds;
} else {
t_seconds = seconds;
}
document.getElementById("timer").innerHTML
= t_hours + ":" t_minutes + ":" t_seconds;
您可以使用How to output integers with leading zeros in JavaScript
的通用键盘功能function pad(num, size) {
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
}
并将您的代码更改为:
document.getElementById("timer").innerHTML = pad(hours,2) + ":" + pad(minutes,2) + ":" + pad(seconds,2);
创建一个类似于以下的函数来为您完成这项工作:
function makeMeTwoDigits(n){
return (n < 10 ? "0" : "") + n;
}
在打印您的号码之前调用此函数:
document.getElementById("timer").innerHTML = makeMeTwoDigits(hours) + ":" + makeMeTwoDigits(minutes) + ":" + makeMeTwoDigits(seconds);
解释:
就像@rfornal 说的那样,我们正在检查数字是否小于 10,这意味着单个数字,添加“0”和 return 否则不添加任何内容并且 return.
需要注意的一点是,如果数字为负数,这将不起作用。