更新时间:2023-12-02 16:18:52
我会找出现在几点,弄清楚距离下一个完整小时还有多久,然后等待那么长时间.所以,
I would find out what time it is now, figure out how long it is until the next full hour, then wait that long. So,
function doSomething() {
var d = new Date(),
h = new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours() + 1, 0, 0, 0),
e = h - d;
if (e > 100) { // some arbitrary time period
window.setTimeout(doSomething, e);
}
// your code
}
检查e >100
只是为了确保您不会在 5 毫秒之类的时间里执行 setTimeout
并陷入疯狂循环.
The check for e > 100
is just to make sure you don't do setTimeout
on something like 5 ms and get in a crazy loop.