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