我在一個頁面中有四個計數器我爲每個函數調用了單獨的函數。我需要將所有函數集成到單個函數中。將四個函數集成到單個函數中
var tt1 = setInterval(function() {
startTimee()
}, 50);
var counter1 = 1;
function startTimee() {
if (counter1 == 10) {
clearInterval(tt1);
} else {
counter1++;
}
document.getElementById('counter1').innerHTML = counter1;
}
var tt = setInterval(function() {
startTime()
}, 5);
var counter = 1;
function startTime() {
if (counter == 600) {
clearInterval(tt);
} else {
counter++;
}
document.getElementById('counter2').innerHTML = counter;
}
var tt2 = setInterval(function() {
startTime2()
}, 50);
var counter2 = 1;
function startTime2() {
if (counter2 == 30) {
clearInterval(tt2);
} else {
counter2++;
}
document.getElementById('count3').innerHTML = counter2;
}
var tt3 = setInterval(function() {
startTime3()
}, 50);
var counter3 = 1;
function startTime3() {
if (counter3 == 6) {
clearInterval(tt3);
} else {
counter3++;
}
document.getElementById('counter4').innerHTML = counter3;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="counter1">10</div>
<div id="counter2">600</div>
<div id="counter3">30</div>
<div id="counter4">6</div>
你試圖讓他們都一樣'setTimeout'代碼下運行? –
耶需要在相同的setInterval下運行 – user3386779