2009-06-05 35 views

答えて

7

いいえ、元の変数を使用することはできません。

21

あなたはの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

私はこの方法が好きです。次に明らかな関数はonUnloadStopTimersです。それが盗まれたと考えてください。 :) –

+8

なぜ 'function'ではなく' new function'ですか? – ThiefMaster

+2

@ThiefMaster彼はJavaの男ですから。 – Sherzod

46

これは、すべてのインターバルをクリアするロジックのいずれかになります...

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

ブルートフォースですが、動作します! –

+2

これを実動コードで使用したくないかもしれませんが、トラブルシューティングの際に対話的に使用することがよくあります。 – undefined

+2

これはChromeで正常に機能します。ありがとうVinay –

2

私はこれを達成した方法は、(アプリケーションレベルのアレイを有することですたとえば、Application.setIntervalIds = [])、setInterval IDを作成するたびにプッシュします。次に、必要に応じて配列の各IDに対してwindow.clearInterval(id)を呼び出すことができます。

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

そして、私は必要なときに、私が呼び出すことができるclearAllSetIntervals機能を持っています:

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

私は(のCoffeeScript)のような何かを書く、新規のsetIntervalを作成する例として

、答え

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

私が探していたものです。この非常にシンプルなロジックを少し改良して、私はこのようなことをすることができました。

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

私が見つけた最良の方法...

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

私はその後をループにこの関数を呼び出すとwindow.clearIntervalで各IDを削除隠し容器に各区間IDを格納しています。..

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'); 
} 

これは醜いですが、私の目的では機能します。

関連する問題