码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-09-25 10:52:29      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

 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="定时警告" onclick="setTimeMsg()"/>
10     <script language="javascript">
11         function setTimeMsg(){
12             setTimeout(‘alert("hello world!!!")‘,3000);//3s
13         }
14     </script>
15     <hr>
16     <!--计数器-->
17     <h1>计数器</h1>
18     <input type="button" value="开始计时" onclick="startTime()"/>
19     <input type="text" id="num"/><br>
20     <input type="button" value="结束计时" onclick="stopTime()"/>
21     
22     <script language="javascript">
23         var i=0;
24         var t;
25         function startTime(){
26             document.getElementById("num").value=i;
27             i=i+1;
28             t=setTimeout("startTime()",10);//1s
29         }
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 onload="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实现计数器,定时警告和停止

标签:

原文地址:http://www.cnblogs.com/biehongli/p/5905092.html

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