码迷,mamicode.com
首页 > Web开发 > 详细

利用setInterval制作网页计时器

时间:2017-07-29 11:31:14      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:ack   charset   date()   暂停   function   一个   val   button   round   

 在javascript中利用 setInterval   clearInterval定时 方法去控制显示时间的增加









1
<!DOCTYPE html> 2 <html> 3 4 <head> 5 <meta charset="UTF-8"> 6 <title>页面时钟</title> 7 <script type="text/javascript" src="js/jquery-3.2.1.js"></script> 8 </head> 9 10 <body> 11 <!--<input type="button" id="button1"value="OK" /> 12 <img src="img/00000.jpg" />--> 13     <!--添加一个文本框显示时间,两个按钮去控制时间的开始和暂停--> 14 <input type="text" size="30" id="one" /><br /> 15 <input type="button" id="two" value="开始" onclick="fun1()" /> 16 <input type="button" id="three" value="结束" onclick="fun2()" /> 17 18 </body> 19 20 </html> 21 <script type="text/javascript"> 22 //控制文本框中的内容 23 // document.getElementById("one").value="123"; 24 var id; 25 26 function fun1() { 27 //确保只打开一个定时器 28 setTime(); 29 30 if(!id) { 31 32 id = window.setInterval(setTime, 1000); 33 } 34 } 35 36 function setTime() { 37 var date = new Date(); 38 document.getElementById("one").value = date.toLocaleString(); 39 } 40 //停止时钟 41 function fun2() { 42 43 clearInterval(id); 44 id = undefined; 45 } 46 </script>

 

利用setInterval制作网页计时器

标签:ack   charset   date()   暂停   function   一个   val   button   round   

原文地址:http://www.cnblogs.com/dygzbj/p/7253595.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!