Take the 2-minute tour ×  How could I accurately run a function
when the minute changes? Using a setInterval could work if I
trigger it right when the minute changes. But I'm worried
setInterval could get disrupted by the event-loop in a long-
running process and not stay in sync with the clock.  How can I
run a function accurately when the minute changes?
share|improve this question  5 Answers 5  up vote 4 down vote
accepted  First off, you should use setInterval for repeating
timers, since it (tries to) guarantee periodic execution, i.e.
any potential delays will not stack up as they will with
repeated setTimeout calls. This will execute your function every
minute:  function f() {   console.log(new Date);  setInterval(f,
60 * 1000);  Now, what we need to do is to start this at the
exact right time:  function repeatEvery(func, interval) {     //
Check current time and calculate the delay until next interval
var now = new Date,         delay = interval - now % interval;
function start() {         //