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
}
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…