JS实现倒计时
Posted 矮子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS实现倒计时相关的知识,希望对你有一定的参考价值。
代码如下:
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实现倒计时的主要内容,如果未能解决你的问题,请参考以下文章