首页 > 代码库 > Javascript实现计数器,定时警告和停止

Javascript实现计数器,定时警告和停止

 1 <html> 2 <head> 3 <meta charset="utf-8"> 4 <title>定时警告</title> 5 </head> 6 <body>  7     <!--定时警告的问题--> 8     <h1>定时警告的问题</h1> 9     <input type="button" value="http://www.mamicode.com/定时警告" onclick="setTimeMsg()"/>10     <script language="javascript">11         function setTimeMsg(){12             setTimeout(‘alert("hello world!!!")‘,3000);//3s13         }14     </script>15     <hr>16     <!--计数器-->17     <h1>计数器</h1>18     <input type="button" value="http://www.mamicode.com/开始计时" onclick="startTime()"/>19     <input type="text" id="num"/><br>20     <input type="button" value="http://www.mamicode.com/结束计时" onclick="stopTime()"/>21     22     <script language="javascript">23         var i=0;24         var t;25         function startTime(){26             document.getElementById("num").value=http://www.mamicode.com/i;27             i=i+1;28             t=setTimeout("startTime()",10);//1s29         }30         31         function stopTime(){32             clearTimeout(t);33         }34     </script>35     36 </body>37 </html>

技术分享

 1 <html> 2 <head> 3 <meta charset="utf-8"> 4 <title>定时警告</title> 5 </head> 6 <body onl oad="startTime()">  7     <h1>秒表<div id="time"></div></h1> 8     <script language="javascript"> 9         function startTime(){10             var date=new Date();11             var year=date.getFullYear();12             var month=date.getMonth()+1;13             var day=date.getDate();14             var hour=date.getHours();15             var minute=date.getMinutes();16             var second=date.getSeconds();17             //var str=year+"年"+month+"月"+day+"日"+hour+":"+minute+":"+second;18             var str=hour+":"+minute+":"+second;19 20             document.getElementById("time").innerHTML=str;21             setTimeout("startTime()",1000);22         }23     </script>24 </body>25 </html>
技术分享

 

Javascript实现计数器,定时警告和停止