Implementation of JavaScript Pause and Continue Timer

  • 2021-07-04 17:59:02
  • OfStack

For the JavaScript timer, there is no pause and restart in the strict sense, only clear stop, but it can be achieved by one'blind eye '


allChild[index].onclick = function(){// When the target is clicked and the following conditions are met   Pause the timer time 
if(gamInt == true){
$("#tu").css("display","block");
// Timer pause (clear timer) 
clearInterval(countdownTimer);
// Delay 2s After the game continues   End of timer pause 
setTimeout(function(){
//  Remove All Pictures 
removeImgFun();
$("#tu").css("display","none");
imgNum++;
if(imgNum >= 10){
imgNum = 10;
}
createImgFun();
// The time before the timer continues to connect increases   Continue to play (re-reference the previous timer 1 Times   Include the name of the timer (must have   Otherwise, it is easy to cause computer crash) completely introduced) 
countdownTimer = setInterval(function (){
countdownNum -= 0.01;
// Keep two decimal places 
countdownNum = countdownNum.toFixed(2);
if (countdownNum <= 0){
clearInterval(countdownTimer);
// Game over 
alert(" Game over! ");
// Fang Zuming can no longer be clicked (scores are no longer counted) 
gamInt = false;
}
time.innerHTML = " Countdown :" + countdownNum + " Seconds ";
},10);
},2000)
// Score 
scores += 1;
score.innerHTML = " No. 1 " + scores + " Guan ";
if(scores > 8){
$("#wc").css("display","block");
$("#haha").css("display","none");
$("#cj").on("click",award());
$("#sm").on("click",explain());
}
}
}

Related articles: