As others have said, go to # 1. If you want a clock that ticks every second (or a minute or something else), you must evaluate the time until the next βtickβ, so setTimeout is called a few ms after the correct time , eg. to start immediately after the next whole second:
var d = new Date(); var interval = 1020 - d.getMilliseconds(); setTimeout(fn, interval);
Thus, if execution for one call is delayed by a busy system, the next should be called approximately 20 ms after the next whole second.
source share