In JavaScript, the setTimeout() function allows you to execute a callback function after a specified delay. However, when you want to call a function repeatedly at a regular interval, you have two options to consider.
If the code within your timer may take longer than your interval, you can use setInterval().
setInterval(function, delay);
This function will repeatedly execute the specified callback function at the given delay.
A more efficient approach is to use setTimeout() along with a self-executing anonymous function:
(function(){ // Execute some code setTimeout(arguments.callee, 60000); })();
This method guarantees that the next call is not made before your code has finished executing. Here, arguments.callee is used as a reference to the function, ensuring proper invocation in setTimeout().
The above is the detailed content of How Can I Call a JavaScript Function at Regular Intervals?. For more information, please follow other related articles on the PHP Chinese website!