JavaScript
Article

JS display and/or stop current setinterval events

By Sam Deering

Use JavaScript to show current setInterval events. Here we have declared a setInterval() without keeping a reference to the id which is returned from the function call (setInterval() returns an id number for the registered event)

var interval = setInterval(function() {
  console.log(‘i’);
}, 1000);
console.log(interval);

It you can see it assigns a random number as the id for the event. Then when we clear the interval it uses that id.

clearInterval(interval);

We can simply do a loop to find the id of the event to stop all setInterval() events that are running.

for(i=0; i<100; i++)
{
  window.clearInterval(i);
}

I’m pretty sure this will also work for setTimeout() and clearTimeout();

Free Guide:

7 Habits of Successful CTOs

"What makes a great CTO?" Engineering skills? Business savvy? An innate tendency to channel a mythical creature (ahem, unicorn)? All of the above? Discover the top traits of the most successful CTOs in this free guide.

No Reader comments

Recommended
Sponsors
Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.