我认为 startTime 只被调用一次,setTimeout 也是如此。为什么这不需要 setInterval?
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
document.getElementById('time').innerHTML =
h + ":" + m + ":" + s;
var t = setTimeout(startTime, 500);
}
function checkTime(i) {
if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
return i;
}
starTime()
最佳答案
函数最后调用自己:
var t = setTimeout(startTime, 500);
每当调用该函数时,都会设置另一个超时。它有效地充当 setInterval,但这样做也允许您仅在特定条件下设置超时。
关于Javascript - 如果没有 setInterval 函数,这个时钟函数如何更新时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54044986/