Home > Web Front-end > JS Tutorial > Implementation method of JavaScript pause and resume timer

Implementation method of JavaScript pause and resume timer

高洛峰
Release: 2017-02-08 15:44:42
Original
2438 people have browsed it

For JavaScript timers, there is no pause and restart in the strict sense, only clear stop, but it can be achieved through some 'eyesores'

allChild[index].onclick = function(){//当点击目标且满足下面的条件时 让计时器时间暂停
if(gamInt == true){
$("#tu").css("display","block");
//计时器暂停(清除定时器)
clearInterval(countdownTimer);
//延迟2s后游戏继续 计时器暂停结束
setTimeout(function(){
// 移除所有的图片
removeImgFun();
$("#tu").css("display","none");
imgNum++;
if(imgNum >= 10){
imgNum = 10;
}
createImgFun();
//定时器继续接之前的时间增加 继续播放(将之前的定时器重新引用一次 包括定时器的名字(必须要有 不然容易引起电脑死机)完整引入)
countdownTimer = setInterval(function (){
countdownNum -= 0.01;
//保留两位小数
countdownNum = countdownNum.toFixed(2);
if (countdownNum <= 0){
clearInterval(countdownTimer);
//游戏结束
alert("游戏结束!");
//房祖名不能再点击(分数不再计数)
gamInt = false;
}
time.innerHTML = "倒计时:" + countdownNum + "秒";
},10);
},2000)
//计分
scores += 1;
score.innerHTML = "第" + scores + "关";
if(scores > 8){
$("#wc").css("display","block");
$("#haha").css("display","none");
$("#cj").on("click",award());
$("#sm").on("click",explain());
}
}
}
Copy after login

The above is the method of pausing and continuing the timer in JavaScript introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!

For more related articles on how to implement JavaScript pause and continue timers, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template