setInterval(): 间隔指定的毫秒数不停地执行指定的代码,定时器
clearInterval(): 用于停止 setInterval() 方法执行的函数代码
使用方法:setInterval(code,millisec),两个参数都是必须的,第一个参数为要调用的函数或要执行的代码串。第二个参数为周期性执行或调用 code 之间的时间间隔,以毫秒计。
clearInterval(id_of_setinterval),参数是必须的,为setInterval返回的ID值
<div id="div_time">00:00:00</div>
<button onclick="start()">开始计时</button>
<button onclick="stop()">结束</button>
<script>
var sec=0;
var intInterval=null;//计时器
function start(){//启动定时器
if(intInterval!=null){//判断计时器是否为空
clearInterval(intInterval);
intInterval=null;
}
intInterval=setInterval(overs,1000);
}
function overs(){
sec++;
var date = new Date(0, 0);
date.setSeconds(sec);
var h = date.getHours(), m = date.getMinutes(), s = date.getSeconds();
$("#div_time").text( two_char(h) + ":" + two_char(m) + ":" + two_char(s));
}
function stop() {
clearInterval(intInterval);
intInterval=null;
}
</script>
原地址:https://blog.csdn.net/YDesire/article/details/81124331?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase
网友评论