我认为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()
答案 0 :(得分:4)
该函数在最后调用自身:
var t = setTimeout(startTime,500);
无论何时调用该函数,都会设置另一个超时。它有效地充当setInterval,但以这种方式进行操作也将仅允许您在某些条件下设置超时。