timer,想必大家都不陌生,我们在很多地方都使用过他。比如计时,闹钟,日期包括一些javascript的动画的实现也离不开它。今天就来简单的介绍下他的用法。
这是一个显示当前时间的演示代码,间隔为1秒。
- //开启timer
- function StartTimer(){
- //定义timer的时间间隔为1000毫秒(也就是1秒)
- mDuration=1000;
- mTestTimer=setInterval("TestFunction()", mDuration);
- }
- //停止timer
- function StopTimer(){
- clearInterval(mTestTimer);
- }
- //执行函数
- function TestFunction(){
- var dateTime=new Date();
- document.getElementById("MainContainer").innerHTML=dateTime.getFullYear() "年" dateTime.getMonth() "月" dateTime.getDay() "日" dateTime.getHours() "时" dateTime.getMinutes() "分" dateTime.getSeconds() "秒";
- }