在這裏,我附上我的代碼。當對話框打開時清除超時功能,但它不能正常工作。當我打開對話框時,倒數從20減少到20秒,如果我關閉並打開對話框,則時間會互相摺疊。clearTimeout功能在打開對話框時無法正常工作
function(){
var time =20;
flag = false;
clearTimeout(startTimer);
startTimer = function(){
if(!flag){
var finTime = time - 1;
time = finTime;
setTimeout(startTimer,1000);
if(time==0){
flag = true; }
$("#input").text(time);
} else {
clearTimeout(startTimer,1000);
}
};
setTimeout(startTimer,1000);
};
我已經試過這個代碼也
dialogOpen = function(){
$("#dialog").dialog('open');
startTimer();
stopTimer();
}
startTimer = function() {
time = 20;
flag = false;
setTimeout(startTime, 1000);
};
stopTimer = function() {
flag = true;
time = 0;
clearTimeout(startTime);
};
startTime = function(){
if(!flag){
var finTime = time - 1;
time = finTime;
setTimeout(startTime,1000);
if(time==0){
flag = true;
}
$("#input").text(time);
} else {
clearTimeout(Time);
}
};
當我一次又一次打開我的對話框時,它並沒有清除以前的時間 – user2587222
'clearTimeout()'不會將函數作爲參數,它需要由'setTimeout()'返回的超時ID。你看過任何['clearTimeout()'文檔](https://developer.mozilla.org/en-US/docs/DOM/window.clearTimeout)嗎? – nnnnnn