2012-12-01 5 views
1

'continue'와 같이 현재 반복을 중단하고 다음 반복으로 진행합니다. JavaScript에서 setInterval() 내에서 현재 반복을 중단하고 기다리지 않고 다음 간격으로 진행하려면 어떻게해야합니까?현재 반복을 중단하고 setInterval() 내에서 다음 반복을 계속하는 방법은 무엇입니까?

var intervalID = window.setInterval(function() { 
    if(conditionIsTrue) { 
     // Break this iteration and proceed with the next 
     // without waiting for 3 seconds. 
    } 
}, 3000); 
+0

이 어디 반복이다. 루프가 있습니까? – polin

+1

우리가하는 일에 대해 조금 더 잘 알 수 있습니까? 지금은 웹 브라우저를 날려 버리는 것처럼 들립니다. –

답변

1

당신은 "간단하게"(또는 그리 간단)의 간격을 취소 할 수 있으며 다시는 만들 :

// run the interval function immediately, then start the interval 
var restartInterval = function() { 
    intervalFunction(); 
    intervalID = setInterval(intervalFunction, 3000); 
}; 

// the function to run each interval 
var intervalFunction = function() { 
    if(conditionIsTrue) { 
     // Break this iteration and proceed with the next 
     // without waiting for 3 seconds. 

     clearInterval(intervalID); 
     restartInterval(); 
    } 
}; 

// kick-off 
var intervalID = window.setInterval(intervalFunction, 3000); 

Here's a demo/test Fiddle.

+0

그래,'invervalFunction'을 자체 함수로 분리하면 모든 것이 더 의미가 있다고 생각합니다. –

관련 문제