2017-06-30 10 views
0

私は2組の関数を持っています。これらの関数は、3秒間(時間の長さ)9回実行する必要があります。各ラン間の遅れは1秒でなければなりません。それぞれの実行後に2つの関数ループを時間遅延でn回作る方法は?

loopIT()関数を短縮するより良い方法はありますか?

window.onload = function() { 
 

 
    // Get the button that will trigger the action 
 
    var b = document.getElementById('trigger'); 
 
    // and set the onclick handler here instead of in HTML 
 
    b.onclick = loopIT; 
 

 
    function startIT() { 
 
    // close switch, turn the light on. 
 
    } 
 
    function stopIT() { 
 
    // open switch, turn the light off. 
 
    } 
 

 
    function loopIT() { 
 
    // Lights on, wait 3 seconds, turn lights off. #1 
 
    setTimeout(startIT, 100); 
 
    setTimeout(stopIT, 3100); 
 
    // Lights on, wait 3 seconds, turn lights off. #2 
 
    setTimeout(startIT, 4100); 
 
    setTimeout(stopIT, 7100); 
 
    // Lights on, wait 3 seconds, turn lights off. #3 
 
    setTimeout(startIT, 8100); 
 
    setTimeout(stopIT, 11100); 
 
    // Lights on, wait 3 seconds, turn lights off. #4 
 
    setTimeout(startIT, 12100); 
 
    setTimeout(stopIT, 15100); 
 
    // Lights on, wait 3 seconds, turn lights off. #4 
 
    setTimeout(startIT, 16100); 
 
    setTimeout(stopIT, 19100); 
 
    // Lights on, wait 3 seconds, turn lights off. #5 
 
    setTimeout(startIT, 22100); 
 
    setTimeout(stopIT, 25100); 
 
    // Lights on, wait 3 seconds, turn lights off. #6 
 
    setTimeout(startIT, 26100); 
 
    setTimeout(stopIT, 29100); 
 
    // Lights on, wait 3 seconds, turn lights off. #7 
 
    setTimeout(startIT, 30100); 
 
    setTimeout(stopIT, 33100); 
 
    // Lights on, wait 3 seconds, turn lights off. #8 
 
    setTimeout(startIT, 34100); 
 
    setTimeout(stopIT, 37100); 
 
    // Lights on, wait 3 seconds, turn lights off. #9 
 
    }   
 
}

+0

何か(I = 0私は++;;私は9 <)のための 'のような支援を期待+ {setTimeoutメソッド(startIT、(4000 * i)を100)。 setTimeout(stopIT、(4000 * i)+ 3100); } ' – Hamms

答えて

0

あなたはループのための2つを使用することができます。

function loopIT() { 
     // goes from #1 to #4 (inclusive) 
     for (var i = 4000; i <= 16000; i += 4000) { 
      setTimeout(startIT, i + 100); 
      setTimeout(stopIT, i + 3100); 
     } 
     // goes from #5 to #9 (inclusive) 
     for (var j = 22000; j <= 34000; j += 4000) { 
      setTimeout(startIT, j + 100); 
      setTimeout(stopIT, j + 3100); 
     } 
    } 
0

setIntervalは、Nミリ秒ごとに特定の機能を実行します。

N回後にカウンタを停止することができます。

// count the number of times we turned the lights on 
 
var cpt = 0; 
 
// turn the lights on right away 
 
lightsOn(); 
 
// turn the lights on every 3 seconds 
 
var intervalOn = setInterval(lightsOn, 3000); 
 

 
function lightsOn() 
 
{ 
 
    console.log("lights on"); 
 

 
    // Turn the lights off in 1 second 
 
    setTimeout(lightsOff, 1000); 
 
} 
 

 
function lightsOff() 
 
{ 
 
    console.log("lights off"); 
 
    ++cpt; 
 
    
 
    // If we've turned off the lights 9 times, stop turning the lights on. 
 
    if (cpt == 9) 
 
    { 
 
    clearInterval(intervalOn); 
 
    } 
 
}

0

私はあなたが(40代について、あなたのケースで)終わりに達するまでいっているがdo-whileループを使用することを提案します。ループごとに1回増加するイテレータを追加することもできます。

function loopIt() { 

    let timeout = 100; 

    do { 
    setTimeout(startIt(), timeout); 
    timeout += 3000; 
    setTimeout(stopIt(), timeout); 
    timeout += 1000; 
    } while (timeout < 40000); 

} 
0

私の提案です:

var repeatedTimes = 0; 
function loopIt(){ 
    if(repeatedTimes < 9){ 
     startIT(); 
     setTimeout(stopIT, 3000); 
     setTimeout(loopIt, 4000); 
    } 
    repeatedTimes++; 
} 
setTimeout(loopIt, 100); 

は、それが

関連する問題