How to Execute a Function Every 60 Seconds
When it comes to invoking a function at a specific time, setTimeout() proves to be a valuable tool. However, it is limited to one-time execution. To overcome this limitation, we often seek a solution that triggers the function multiple times at regular intervals.
Enter setInterval(), which stands as a reliable option if the execution of the function does not consume more time than the specified interval. Here's how to use it:
setInterval(function, delay)
Alternatively, we can employ a more refined approach by combining setTimeout() with a self-executing anonymous function as follows:
(function(){ // do some stuff setTimeout(arguments.callee, 60000); })();
This approach ensures that subsequent calls are deferred until the preceding operation is complete. It is considered superior to using arguments.callee, which is deprecated in ECMAScript 5.
The above is the detailed content of How to Repeatedly Execute a JavaScript Function Every 60 Seconds?. For more information, please follow other related articles on the PHP Chinese website!