JavaScript
Article
By Sam Deering

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

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