'JavaScript setInterval Limits?
I have an application using JavaScript's setInterval() to run a digital clock. I was wondering if it has a timeout, or limit, to the amount of times it can execute this function.
Solution 1:[1]
setInterval() will run infinitely.
If you wish to terminate the 'loop' you can use clearInterval. For example:
var counter = 0;
var looper = setInterval(function(){
counter++;
console.log("Counter is: " + counter);
if (counter >= 5)
{
clearInterval(looper);
}
}, 1000);
Solution 2:[2]
As others have mentioned, there is not limit to the number of times your interval will run, however if you intend to run a timer indefinitely you should consider the info here:
Minimum setInterval()/setTimeout() delay on background tabs
If your user is likely to tab-out, 1 second seems to be safe minimum interval
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | rochal |
| Solution 2 | Community |
