JavaScript
Article
By Sam Deering

JS display and/or stop current setinterval events

By Sam Deering
Last chance to win! You'll get a... FREE 6-Month Subscription to SitePoint Premium Plus you'll go in the draw to WIN a new Macbook SitePoint 2017 Survey Yes, let's Do this It only takes 5 min

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

--ADVERTISEMENT--

Login or Create Account to Comment
Login Create Account
Recommended
Sponsors
Get the most important and interesting stories in tech. Straight to your inbox, daily.
Is it good?Is it good?