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

JS实现倒计时

时间:2020-05-02 22:38:01      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:dtd   show   func   body   class   client   http   nal   span   

 

代码如下:

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html >
 3 <head>
 4     <style type="text/css">
 5         div{
 6             font-size:58px;
 7         }
 8     </style>
 9     <meta charset="utf-8">
10     <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
11     <title>JS时间倒计时</title>
12     <script type="text/javascript">
13         var time_now_server,time_now_client,time_end,time_server_client;
14  
15         time_end=new Date("2021/11/11 00:00:0");         //结束的时间
16         time_end=time_end.getTime();//获取的是毫秒
17  
18         time_now_server=new Date();//开始的时间
19         time_now_server=time_now_server.getTime();
20         setTimeout("show_time()",1000);
21  
22         function show_time()
23         {
24             var timer = document.getElementById("timer");
25             var hourid = document.getElementById("hour");
26             if(!timer){
27                 return ;
28             }
29             timer.innerHTML =time_now_server;
30  
31             var time_now,time_distance,str_time;
32             var int_day,int_hour,int_minute,int_second;
33             var time_now=new Date();
34             time_now=time_now.getTime();
35             time_distance=time_end-time_now;
36             if(time_distance>0)
37             {
38                 int_day=Math.floor(time_distance/86400000)
39                 time_distance-=int_day*86400000;
40                 int_hour=Math.floor(time_distance/3600000)
41                 time_distance-=int_hour*3600000;
42                 int_minute=Math.floor(time_distance/60000)
43                 time_distance-=int_minute*60000;
44                 int_second=Math.floor(time_distance/1000)
45  
46                 if(int_hour < 10)
47                     int_hour="0"+int_hour;
48                 if(int_minute<10)
49                     int_minute="0"+int_minute;
50                 if(int_second<10)
51                     int_second="0"+int_second;
52                 str_time=int_day+"天"+int_hour+"小时"+int_minute+"分钟"+int_second+"秒";
53                 timer.innerHTML=str_time;
54                 setTimeout("show_time()",1000);
55             }
56             else
57             {
58                 timer.innerHTML =0;
59             }
60         }
61     </script>
62 </head>
63  
64 <body>
65 <div id="timer"></div>
66 </body>
67 </html>

 

JS实现倒计时

标签:dtd   show   func   body   class   client   http   nal   span   

原文地址:https://www.cnblogs.com/huangshengpeng/p/12819887.html

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