js学习-定时器的使用
定时器-js获取当前时间
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta charset="utf-8">
- <title>定时器-js获取当前时间</title>
- <script>
- window.onload=function()
- {
- //个位数变十位数
- function toDouble(n)
- {
- if(n<10)
- {return '1'+n;}
- else
- {return ''+n;}
- };
- var aSpan=document.getElementsByTagName('span');
- function tick()
- {
- //获取当前时间
- var oDate=new Date();
- var str=toDouble(oDate.getHours())+toDouble(oDate.getMinutes())+toDouble(oDate.getSeconds());
- for(var i=0;i<aSpan.length;i++)
- {
- //aSpan[i].innerHTML=str[i];//ie7不兼容
- //str[i]与str.charAt(i)的区别
- aSpan[i].innerHTML=str.charAt(i);
- }
- }
- //定时器
- setInterval(tick,1000)
- tick();//去掉延迟
- };
- </script>
- </head>
- <body>
- <span>0</span><span>0</span>
- :
- <span>0</span><span>0</span>
- :
- <span>0</span><span>0</span>
- </body>
- </html>
延时提示框
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta charset="utf-8">
- <title>延时提示框</title>
- <style>
- div{float: left;margin:10px;}
- #div1{width: 50px;height: 50px;background: red;}
- #div2{width: 250px;height: 180px;background: #ccc;display: none;}
- </style>
- <script>
- window.onload=function()
- {
- var oDiv1=document.getElementById('div1');
- var oDiv2=document.getElementById('div2');
- var timer=null;//全局应用
- //合并 连等
- oDiv1.onmouseover=oDiv2.onmouseover=function()
- {
- clearTimeout(timer);//去除定时器效果
- oDiv2.style.display='block';
- };
- oDiv1.onmouseout=oDiv2.onmouseout=function()
- {
- //延时
- timer=setTimeout(function(){
- oDiv2.style.display='none';
- },500);
- };
- };
- </script>
- </head>
- <body>
- <div id="div1"></div>
- <div id="div2"></div>
- </body>
- </html>