How can I clearInterval() for all setInterval()?
Asked Answered
M

9

50

I've got a setInterval() called in a jQuery plugin, but I want to clear it from my main page, where I don't have access to the variable that the setInterval was stored in.

Is there a way to clear all timers present on a page?

Mcnary answered 5/6, 2009 at 22:26 Comment(0)
B
68

This can be one of logic to clear all interval...

for (var i = 1; i < 99999; i++)
        window.clearInterval(i);
Bunny answered 21/8, 2011 at 3:0 Comment(2)
You probably wouldn't want to use this in production code, but I often use it interactively when troubleshooting.Lacework
is there any info on which maximum value for interval ID will be assigned in different browsers? if i recall correctly some browsers assign values consequentially from 1 and some assign random values? not sure of thisRapid
O
26

You can override setInterval:

window.oldSetInterval = window.setInterval;
window.setInterval = function(func, interval) {
    var interval = oldSetInterval(func, interval);
    // store it in a array for stopping? stop it now? the power is yours.
}
Overwork answered 5/6, 2009 at 22:57 Comment(4)
I like this method. The next obvious function is onUnloadStopTimers. Consider it stolen. :)Arabist
Why new function instead of function?Hypertrophy
@Hypertrophy because he's the Java guy.Newburg
@Jakemmarsh Of course there's a naming collision. That's the whole point of this solution. The idea is to overwrite the setInterval function with something better.Discriminating
V
10

No, you can't, not without the original variable.

Versed answered 5/6, 2009 at 22:27 Comment(0)
K
7

The answer

for (var i = 1; i < 99999; i++)
     window.clearInterval(i);

was the one I was looking for. With a little improvement of this very simple logic, I was able to do something like this.

var i = 0;
var rotatorId;
var rotator;

rotator =  setInterval(function() {myfunction(), 3000});
rotatorId[i] = rotator;
i++;

if (rotator > 1) {
   for(i = 1; i < rotatorId.length; i++){
      clearInterval(rotatorId[i]);                      
    }                       
}
Kiethkiev answered 9/11, 2013 at 12:52 Comment(0)
P
5

The way I have achieved this is by having an application level array (e.g., Application.setIntervalIds = []) to which I push the setInterval ids to whenever one is created. Then I can simply call window.clearInterval(id) on each id in the array when I need to.

As an example, when I create a new setInterval I write something like (coffeescript):

id = setInterval (() -> function2call()), timeout
Application.setIntervalIds.push id

And then I have a clearAllSetIntervals function I can call when needed:

Application.clearAllSetIntervals = () ->
    $.each Application.setIntervalIds, (index, id) ->
         window.clearInterval id
Poyang answered 21/8, 2013 at 12:21 Comment(0)
U
3

Best way i found ...

var clearAllIntervals = function ( ) {

    var intervals = [];

    $(".elements").each(function() {
        intervals.push( setInterval(function() {

        }, 1000) );
    });

    return function clearAll ( ) {
        intervals.forEach( clearInterval );
    }

}( );

// When you want to clear them:
clearAllIntervals( );
Unceasing answered 18/5, 2017 at 10:38 Comment(0)
I
1

I'm storing each interval id in a hidden container then calling this function to loop through and remove each id with window.clearInterval..

function clearAllIntervals() {
    $('.intervals-holder span').each(function() {
        var clearThis = parseInt($(this).text(), 10); // gets the interval id from the span holder
        window.clearInterval(clearThis); // removes the interval
        $(this).remove(); // removes the span holding the id
    })
}

// setting interval
// (i clear all first because in my code the buttons can be double clicked..
// ..and there is more than one button to start what i want to be started)
function audioRingingInterval() {
    clearAllIntervals();
    var intervalId = setInterval(audioRingingStartFunction, 500);
    $('.intervals-holder').append('<span>'+intervalId+'</span>');
}
// i call this when i want to set the interval and it calls the interval function above
function audioRingingStartFunction() {
    $('.audio-blinking').toggleClass('blinking');
}
// i call this when i want to stop the interval
function audioRingingStopFunction() {
    clearAllIntervals();
    $('.audio-blinking').removeClass('blinking');
}

It's ugly but for my purposes it works.

Isaiasisak answered 16/2, 2018 at 5:24 Comment(0)
M
1

This worked for me.

//Setting interval
var startInterval = setInterval(function () {
 //interval code here
}, 1000);

//Clearing interval
var countInterval = startInterval != undefined ? startInterval : 0;

 for (var a = 0; a < countInterval; a++) {
  clearInterval(a);
 }
Myramyrah answered 20/4, 2018 at 10:45 Comment(0)
S
0

Inserting this code will allow the use of clearAllInterval();. When clearAllInterval(); is executed, clearInterval is applied to all running setIntervals.

window.intervallib=[];
    window.clearAllInterval=function(){
        for(i=0;i<window.intervallib.length;i++){
        clearInterval(window.intervallib[i]);
        }
        window.intervallib=[];
    }
    window.oldSetInterval = window.setInterval;
    window.setInterval = function(func, interval) {
        var id=oldSetInterval(func, interval)
        window.intervallib.push(id);
        return id;
    }
Selfsatisfaction answered 16/7, 2022 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.