2009-06-05 10 views

Respuesta

7

No, no se puede, no sin la variable original.

21

Puede anular 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. 
} 
+0

me gusta este método. La siguiente función obvia es onUnloadStopTimers. Considéralo robado. :) –

+8

¿Por qué 'nueva función' en lugar de' función'? – ThiefMaster

+2

@ThiefMaster porque él es el chico de Java. – Sherzod

46

Esta puede ser una de la lógica para borrar toda intervalo ...

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

Fuerza bruta, ¡pero funciona! –

+2

Probablemente no desee utilizar esto en el código de producción, pero a menudo lo uso de forma interactiva cuando soluciono problemas. – undefined

+2

¡Esto funciona bien en Chrome! Gracias Vinay –

2

La forma en que he logrado esto es por tener una gran variedad de nivel de aplicación (por ejemplo, Application.setIntervalIds = []) a la que presiono los identificadores setInterval cada vez que se crea uno. Entonces, simplemente puedo llamar a window.clearInterval (id) en cada identificación de la matriz cuando sea necesario.

A modo de ejemplo, cuando se crea un nuevo setInterval que escribir algo como (CoffeeScript):

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

Y luego tengo una función clearAllSetIntervals puedo llamar cuando sea necesario:

Application.clearAllSetIntervals =() -> 
    $.each Application.setIntervalIds, (index, id) -> 
     window.clearInterval id 
2

El respuesta

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

era el que estaba buscando. Con un poco de mejora de esta lógica tan simple, pude hacer algo como esto.

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

mejor manera que encontré ...

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(); 
0

Estoy almacenando cada uno Identificación del intervalo en un contenedor oculto a continuación, llamar a esta función a través del bucle y quitar cada id con 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'); 
} 

Es feo pero para mi propósito funciona.