43

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?

8 Answers8

62

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

for (var i = 1; i < 99999; i++)
        window.clearInterval(i);
Vinay
  • 2,514
  • 3
  • 24
  • 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
25

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
  • 344,413
  • 96
  • 627
  • 693
Isaac Waller
  • 32,449
  • 28
  • 95
  • 107
10

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

Sasha Chedygov
  • 122,245
  • 26
  • 99
  • 112
6

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,217
  • 1
  • 24
  • 32
Marcus Ford
  • 61
  • 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
  • 3,878
  • 2
  • 31
  • 46
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
  • 751
  • 6
  • 14
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,082
  • 3
  • 31
  • 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
  • 723
  • 10
  • 28