JavaScript setInterval Limits?

Ethan Turkeltaub picture Ethan Turkeltaub · Oct 20, 2011 · Viewed 19.9k times · Source

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.

Answer

rochal picture rochal · Oct 20, 2011

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);