jquery定时器

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。