javascript定时器小结

页面导航:首页 > 网络编程 > JavaScript > javascript定时器小结

javascript定时器小结

来源: 作者: 时间:2016-02-01 09:32 【

javascript有两种定显示器, setTimeout和setInterval,下面简要介绍两个函数然后附上简单的例子。setTimeout(function, time) , 当定位到time间隔则执行function函数,执行一次就不再执行clearTimeout(t)

javascript有两种定显示器, setTimeout和setInterval,下面简要介绍两个函数然后附上简单的例子。
setTimeout(function, time) , 当定位到time间隔则执行function函数,执行一次就不再执行

clearTimeout(t), 结束setTimeout定时器
setInterval(function, time), 每隔time间隔执行一次function函数

clearInterval(t),结束setinterval定时器
示例:

 





定时器

<script type=text/javascript>|r| var c1 = 0;|r| var c2 = 0;|r| var t; |r| var t2;|r| var t3;|r| function timeCount(){|r| document.getElementById('txt').value = c1;|r| c1 = c1 + 1;|r| t = setTimeout(timeCount(), 1000); //此处实现timeCount函数每隔1s执行一次的功能 |r| }|r| function stopTimeout(){|r| clearTimeout(t);|r| }|r| function timeCount2(){|r| document.getElementById('txt').value=c2;|r| c2=c2+1;|r| }|r| function interval(){|r| t2 = setInterval(timeCount2(), 1000);|r| }|r| function stop(){|r| clearInterval(t2);|r| }|r| function startTime(){|r| var today = new Date(); |r| var y = today.getFullYear();|r| var mon= today.getMonth();|r| var day = today.getDate();|r| var h = today.getHours();|r| var m = today.getMinutes();|r| var s = today.getSeconds();|r| m = checkTime(m);|r| s = checkTime(s);|r| document.getElementById('txt').value = y+-+mon+-+day+ +h+:+m+:+s;|r| //t3 = setTimeout('startTime()',1000); |r| }|r| function start(){|r| t3 = setInterval('startTime()',1000); |r| }|r| |r| function stopTime(){|r| //clearTimeout(t3);|r| clearInterval(t3);|r| }|r| function checkTime(i){|r| if (i<10){|r| i = 0+i;|r| }|r| return i;|r| }|r| |r| </script>





实时时钟


 

 

示例效果:

data-cke-saved-src=/uploads/allimg/c160122/14534230WB450-11a4.png

 

Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<