47

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?

9 Answers9

66

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

for (var i = 1; i < 99999; i++)
        window.clearInterval(i);
Vinay
  • 2,564
  • 4
  • 26
  • 35
  • 3
    You probably wouldn't want to use this in production code, but I often use it interactively when troubleshooting. – undefined Jul 13 '12 at 22:38
  • 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 this – llamerr Nov 14 '12 at 17:31
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.
}
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
Isaac Waller
  • 32,709
  • 29
  • 96
  • 107
10

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

Sasha Chedygov
  • 127,549
  • 26
  • 102
  • 115
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]);                      
    }                       
}
Nikos
  • 3,267
  • 1
  • 25
  • 32
Marcus Ford
  • 71
  • 1
  • 1
3

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
ProfNimrod
  • 4,142
  • 2
  • 35
  • 54
2

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( );
Ashwini Jindal
  • 811
  • 8
  • 15
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.

Hastig Zusammenstellen
  • 4,286
  • 3
  • 32
  • 45
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);
 }
Salman Ullah Khan
  • 730
  • 10
  • 29
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;
    }