JavaScript setInterval Limits?

I have an application using JavaScript setInterval() to start a digital clock. I was wondering if it has a timeout or a limit on the number of times it can perform this function.

+6
source share
3 answers

No, this function will continue until you clear the interval manually with clearInterval()

Please note that in most browsers your function will still be executed when the page is on the background tab, but mobile browsers (in particular, iOS5 Safari) can free the page until it is focused / visible again.

+3
source

setInterval() will work endlessly.

If you want to end the loop, you can use clearInterval. For instance:

 var counter = 0; var looper = setInterval(function(){ counter++; console.log("Counter is: " + counter); if (counter >= 5) { clearInterval(looper); } }, 1000); 
+13
source

As already mentioned, there is no limit to the number of times your interval will work, however, if you start the timer indefinitely, you should consider the information here:

Minimum delay setInterval () / setTimeout () on background tabs

If your user is likely to select a tab, 1 second seems to be a safe minimum interval

0
source

Source: https://habr.com/ru/post/899705/


All Articles