:
あなたはたとえば以下の定義済みperiods.Theに何度も何度も実行されますのsetIntervalを使用することができますが、すべての2番目の 'こんにちは' に印刷されます。次を使用する必要があるのsetIntervalを停止する
var myID = setInterval(function(){
console.log('Hello there');
},1000);
:
のみ
のsetTimeout使用
clearInterval(myID);
:またのsetTimeoutを使用し、その機能を呼び出すことができます
をそれ自身を呼び出すでしょう。以下の例では、の1秒後にを実行してから、コンソールに「Hello There」という2回目の印刷を繰り返します。 の両方を使用して
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を組み合わせることができます。次の例では、の1秒が経過した後、毎秒の "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