稱爲此代碼應結束前10秒時運行,但如果您再次運行功能,10秒鐘完成之前,應該明確theTimeout一遍又一遍clearTimeout不工作的if語句
開始10秒function start() {
let counter = 0;
let timeUp = true;
let hello;
setInterval(()=> {
counter++
console.log(counter)
},1000);
if (timeUp == false) {
clearTimeout(hello)
timeUp = true
console.log('should run again with new clock')
start()
} else {
console.log('new clock started')
timeUp = false;
hello = setTimeout(() => {
timeUp = true
console.log('end clock')
}, 10000);
};
};
這並不實現什麼OP問。兩次調用'myTimer.start'不會重置計時器。 – bejado
@bejado True。我做了一個編輯,所以你可以多次啓動它 – blex