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?
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?
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.
}
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]);
}
}
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);
}
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;
}