2016-09-05 2 views
-2

console.log();은 시간이 만료 된 후에 만 ​​실행 한 다음 반복하지만 페이지로드 즉시 실행하지 않습니다. 이것이 어떻게 완성 되었습니까? 단지 setInterval을 사용setTimeout을 사용하여 첫 번째 발생을 건너 뜁니다.

function setTimer(t) { 
    (function timeout() { 
    console.log('timer '+t+' seconds') 
    setTimeout(timeout, t*1000); 
    })(); 
} 

setTimer(5); 
setTimer(10); 
setTimer(15); 
setTimer(20); 

https://jsfiddle.net/y969uLhx/

+1

'document.ready' 콜백과 함께 ['setInterval'] (https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval)을 사용하십시오. –

+0

@ ConstantinAzizov'setInterval'에는 문제가 있으며 사용하고 싶지 않습니다. – user1032531

+0

인하해야하는 이유는 무엇입니까? – user1032531

답변

1

:

당신은 예를 아래에 미리 정의 된 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); 
 
    }
[정보

+0

두 번째 예제 인'setTimeout()'을 사용하여 어떻게 완성 할 수 있습니까? 즉각적으로 실행됩니다. 즉, 특히 예방하고 싶습니다. – user1032531

+0

setTimeOut은 전달한 값에 따라 실행됩니다. 예제에서 setTimeOut은 호출 된 후 1 초가 걸린다. 그럼에도 불구하고 여러분의 필요에 맞게 조정할 수있는 코드를 추가했습니다. 두 번째 예를 확인하십시오. – Strahdvonzar

+0

Humm 두 개의'setTimeout()'함수. 조금 복잡해 보입니다. 첫 번째 방법을 사용하지만 약간 수정 될 수 있습니다. https : // jsfiddle.net/2f0kq2u8 /. 잘 모르겠지만 내가 설정 한 임의의 수의 타이머가있는 예제 (예를 들어, 5 초, 10 초, 15 초 및 20 초를 보여 주지만 연속적인 시간대에 더 많거나 적음)). – user1032531

0

무엇을 다음 :

function MyLoop(t) { 
    setTimeout(function() { 
    console.log('Loop '+t+' seconds.') 
    MyLoop(t); 
    }, t * 1000); 
} 

MyLoop(5); 
MyLoop(10); 
MyLoop(15); 
MyLoop(20); 
관련 문제