2013-06-19 130 views
0

我希望我的倒計時停止點擊提交按鈕,我搜索了一些頁面, 但我沒有找到任何東西。 這裏是我想停止點擊停止倒計時

function countDown (count) { 
    if (count > 0) { 
    var d = document.getElementById("countDiv"); 
    d.innerHTML = count; 
    setTimeout (function() { countDown(count-1); }, 1000); 
    document.getElementById('tiempo').value = count; 
    } 
    else 
    document.location = "timeover.php"; 
} 
document.getElementById("palabra").focus(); 
countDown(5); 
</script> 
+0

你的代碼是做什麼的?你期望它做什麼呢?提交按鈕在哪裏?你的問題在哪裏? – Teemu

回答

0

嘗試是這樣的代碼:

var stopped = false; 
function countDown (count) { 
    if (count > 0) { 
    var d = document.getElementById("countDiv"); 
    d.innerHTML = count; 
    document.getElementById('tiempo').value = count; 
    if (!stopped) { 
    setTimeout (function() { countDown(count-1); }, 1000); 
    } 
    } 
    else 
    document.location = "timeover.php"; 
} 
document.getElementById("palabra").focus(); 
document.getElementById("mySubmitId").onclick = function() { 
    stopped = true; 
}; 
countDown(5); 
+0

「count」的遞減量在哪裏? )? – Teemu

+0

@Teemu但倒計時應該停止。 –

+1

我喜歡它,但似乎OP想要顯示元素中的剩餘時間('count')。在你的代碼中,這個「顯示」保持在'4'。 – Teemu

0

你必須保存參照超時(實際上返回超時值將是數)並用它來取消超時。

var timeout = window.setTimeout(function() { 
    // do something 
}, 1000); 

// clear timeout 
window.clearTimeout(timeout); 

你可能有想法。順便說一句,你應該看看setInterval方法,因爲在這種情況下它會更好。間隔將「打勾」,直到您取消爲止。