Using setTimeout() it is possible to launch a function at a specified time:
setTimeout(function, 60000);
But what if I would like to launch the function multiple times? Every time a time interval passes, I would like to execute the function (every 60 seconds, let’s say).
If you don’t care if the code within the
timermay take longer than your interval, usesetInterval():That fires the function passed in as first parameter over and over.
A better approach is, to use
setTimeoutalong with aself-executing anonymousfunction:that guarantees, that the next call is not made before your code was executed. I used
arguments.calleein this example as function reference. It’s a better way to give the function a name and call that withinsetTimeoutbecausearguments.calleeis deprecated in ecmascript 5.