Home > Web Front-end > JS Tutorial > How to Repeatedly Execute a JavaScript Function Every 60 Seconds?

How to Repeatedly Execute a JavaScript Function Every 60 Seconds?

Mary-Kate Olsen
Release: 2024-12-03 05:03:09
Original
598 people have browsed it

How to Repeatedly Execute a JavaScript Function Every 60 Seconds?

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)
Copy after login

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);
})();
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template