javascript定时器如何暂停及继续
2个回答
2018-05-29
展开全部
// html
<div>当前时间为:<span id="timeNow"></span> </div>
<button id="timeBegin">计时开始</button>
<button id="timeEnd">计时结束</button>
<button id="timeClear">计时清除</button>
// Javascript
<script type="text/javascript">
//定义初始值 计时器
var count = 0;
var timer = "";
//开始计时
function BeginTime(){
var beginBtn = document.getElementById("timeBegin");
beginBtn.onclick = function(){
timer = setInterval(function(){
count ++ ;
document.getElementById("timeNow").innerHTML = count / 100;
},10)
}
}
//结束计时
function EndTime(){
var endBtn = document.getElementById("timeEnd");
endBtn.onclick = function(){
clearInterval(timer);
}
}
//计时清除
function ClearTime(){
var clearBtn = document.getElementById("timeClear");
clearBtn.onclick = function(){
document.getElementById("timeNow").innerHTML = "";
}
}
BeginTime();
EndTime();
ClearTime();
</script>
// The Harder You Work, The Luckier You Will Be. (Jensonhui.com)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询