:
당신은 예를 아래에 미리 정의 된 periods.The 다시하고 다시 실행합니다 setInterval을를 사용할 수는 매초마다 '안녕하세요'인쇄됩니다. 단지 에서는 setTimeout 사용
clearInterval(myID);
:
var myID = setInterval(function(){
console.log('Hello there');
},1000);
는 setInterval을 중지하려면 다음 사용해야 또한 의 setTimeout을 사용하고 함수를 호출 할 수 있습니다
을 그 자체를 호출합니다. 아래의 예는 1 초 후에 을 실행하고을 두 번 반복하여 콘솔에 "Hello There"라는 인쇄를 반복합니다.
setTimeout(function(){
CallMyself();
},
// Set the Execution to take place exactly 1000 ms after it has been called.
1000);
function CallMyself(){
console.log('Hello There');
setTimeout(function(){
CallMyself();
},
// Set the period of each loop to be 1000 ms
1000);
}
모두을 결합 사용 :
또한 setInterval을 및 의 setTimeout을 결합 할 수 있습니다. 아래 예는 초 후에 초마다 "Hello There"를 인쇄하기 시작합니다.
setTimeout(function(){
setInterval(function(){
console.log('Hello there');
},1000);
},1000);
서로 다른 타이머 만들 수 있도록 인수로 초기 지연 및 루프 지연을받는 함수의 예 :
// First argument is the Delay in Execution.
// Second argument is the period it takes for each loop to be completed.
setCustomTimer(1 , 2);
setCustomTimer(2 , 4);
function setCustomTimer(initialDelay , LoopDelay){
console.log('Initial Delay of ' + initialDelay + ' Seconds');
var myDelay = LoopDelay;
setTimeout(function(){
console.log('Initial Delay of ' + initialDelay + ' seconds is Over. Starting Loop. ');
CallMyself(myDelay);
},initialDelay * 1000);
}
function CallMyself(LoopDelay){
console.log('Loop every ' + LoopDelay + ' Seconds');
setTimeout(function(){
CallMyself(LoopDelay);
},LoopDelay * 1000);
}
[정보
'document.ready' 콜백과 함께 ['setInterval'] (https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval)을 사용하십시오. –
@ ConstantinAzizov'setInterval'에는 문제가 있으며 사용하고 싶지 않습니다. – user1032531
인하해야하는 이유는 무엇입니까? – user1032531