我無法暫停我的倒數計時器。我試圖做到這一點後,我點擊播放按鈕,按鈕變成一個暫停按鈕。但由於某種原因,它不會暫停。CountDown計時器:暫停按鈕不會暫停或變回播放按鈕
我試圖將id「pButton」更改爲「pauseButton」,它正在工作,但似乎我的暫停onclick函數不會更改回pButton,有任何幫助嗎?(clearinterval(timecounter),timercounter是全球varaible所以它可以訪問),另外這裏是代碼筆的完整代碼,如果你需要看到的是:http://codepen.io/freefora11/pen/eJdVjZ
//when play button is pressed
if(document.getElementById("pButton")){
document.getElementById("pButton").onclick=function(){
//change the id of the button
if(document.getElementById("pButton")){
document.getElementById("pButton").id = "pauseButton";
}
//variables
strMin = document.getElementById("test").innerHTML;
var res = strMin.split(":",2);
min = parseInt(res[0]);
min= min * 60;
var sec = parseInt(res[1]);
timeInSeconds = min + sec;
//set interval to start keeping track each second that has passed
timeCounter = setInterval(function() {
timeInSeconds--;
// If we hit 0 seconds clear the timer
if (timeInSeconds <= 0) {
clearInterval(timeCounter);
}
// Display the current time
displayTime();
}, 1000);
}
}
//when the pause button is clicked
if(document.getElementById("pauseButton")){
document.getElementById("pauseButton").onclick=function(){
//stop the interval
clearInterval(timeCounter);
//change the id of the play button from pauseButton to pButton again
if(document.getElementById("pauseButton")){
document.getElementById("pauseButton").id ="pButton";
}
}
}
更改ID似乎是一個可怕的想法,你應該改變類。也只能使用一個點擊處理程序,做你的狀態檢查在一個處理程序。 – Musa
這花了一段時間,但我能弄清楚如何把它放到一個處理程序,謝謝 – jediderek