2009-06-05 3 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 그는 자바 사람이기 때문에. – 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)을 호출하면됩니다.

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

: 내가 필요로 할 때 내가 호출 할 수있는 clearAllSetIntervals 기능이

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

: 그리고 나는 새로운 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

I는 다음 루프를 통해이 기능을 호출하고 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'); 
} 

내 생각에는 효과가 있습니다.

관련 문제