js笔记(9)之定时器&&数字时钟&&延时提示框
2016-04-02 20:57
417 查看
定时器 function show(){ alert('a'); } setInterval(show,1000); 函数,毫秒(间隔型、无限次执行) setTimeout(show,1000); (延时型、只执行一次) clearInterval clearTimeout ------------------------------------------------------------ <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>定时器的开启和关闭</title> <script type="text/javascript"> window.onload = function(){ var oBtn1 = document.getElementById('btn1'); var oBtn2 = document.getElementById('btn2'); var timer = null; oBtn1.onclick = function(){ timer = setInterval(function(){ alert('a'); },1000); } oBtn2.onclick = function(){ clearInterval(timer); } } </script> </head> <body> <input id="btn1" type="button" value="开启" /> <input id="btn2" type="button" value="关闭" /> </body> </html> ---------------------------------------------------------------------- 获取当前时间 var oDate = new Date(); alert(getHours()); getMinutes(); getSeconds(); <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>数码时钟</title> <style type="text/css"> </style> <script type="text/javascript"> function toDou(n){ if(n < 10) return '0' + n; else return '' + n; } window.onload = function(){ var aImg = document.getElementsByTagName('img'); function tick(){ var oDate = new Date(); var str = toDou(oDate.getHours()) + toDou(oDate.getMinutes()) +toDou(oDate.getSeconds()); for(var i = 0;i < aImg.length;i++){ aImg[i].src = 'img/'+str.charAt(i)+'.jpg'; } } setInterval(tick,1000); tick(); } </script> </head> <body style="background: #262626; color: white; font-size: 50px" > <img src="img/0.jpg"> <img src="img/0.jpg"> : <img src="img/0.jpg"> <img src="img/0.jpg"> : <img src="img/0.jpg"> <img src="img/0.jpg"> </body> </html> ------------------------------------------------------------------------------- 获取年月日 var oDate = new Date(); oDate.getFullYear();//年 oDate.getMonth()+1;//月 oDate.getDate();//哪一天 oDate.getDay();//当前星期几 --------------------------------------------------------------------------------- 延时提示框 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>延时提示框</title> <style type="text/css"> div{ float: left; margin: 10px; } #div1{ width: 50px; height: 50px; background: red; } #div2{ width: 250px; height: 180px; background: #CCC; display: none; } </style> <script type="text/javascript"> window.onload = function(){ var oDiv1 = document.getElementById('div1'); var oDiv2 = document.getElementById('div2'); var timer = null; oDiv1.onmouseover = function(){ clearTimeout(timer); oDiv2.style.display = 'block'; } oDiv1.onmouseout = function(){ timer = setTimeout(function(){ oDiv2.style.display = 'none'; },500); } oDiv2.onmouseover = function(){ clearTimeout(timer); } oDiv2.onmouseout = function(){ timer = setTimeout(function(){ oDiv2.style.display = 'none'; },500); } } </script> </head> <body> <div id="div1"></div> <div id="div2"></div> </body> </html> --------------------------------------------------- <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>延时提示框之函数合并</title> <style type="text/css"> div{ float: left; margin: 10px; } #div1{ width: 50px; height: 50px; background: red; } #div2{ width: 250px; height: 180px; background: #CCC; display: none; } </style> <script type="text/javascript"> window.onload = function(){ var oDiv1 = document.getElementById('div1'); var oDiv2 = document.getElementById('div2'); var timer = null; oDiv2.onmouseover = oDiv1.onmouseover = function(){ clearTimeout(timer); oDiv2.style.display = 'block'; } oDiv2.onmouseout = oDiv1.onmouseout = function(){ timer = setTimeout(function(){ oDiv2.style.display = 'none'; },500); } } </script> </head> <body> <div id="div1"></div> <div id="div2"></div> </body> </html>
相关文章推荐
- 浅谈JS中对象、函数的__proto__属性和prototype对象
- JavaScript遍历方式详解
- 【笔记】 《js权威指南》- 第6章 对象 - 6.5 - 6.6 属性操作2
- 如何使用ajax将json传入后台数据
- js保留小数点后面几位的方法
- 【笔记】 《js权威指南》- 第6章 对象 - 6.2 - 6.4 属性操作1
- javaScript树形结构
- js中for in 和 for each in的使用
- js设计模式-组合模式
- Chrome 插件JSONView改变风格
- js设计模式-桥接模式
- 【笔记】 《js权威指南》- 第6章 对象 - 6.1 创建对象
- js笔记(8)之数组篇
- jsp导入包的时候要写的规范
- JavaScript代码模块化的正规方法
- [JSP] c:forEach 如何输出序号
- 纯JS实现中国行政区域上下联动选择地址
- 《JS高程》事件学习笔记
- 常规功能和模块自定义系统 (cfcmms)—037模块记录(数据)的权限设计(2)
- 模板引擎 Handlebars.js