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() {
}, 1000);

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.


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

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


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