JS倒计时代码-精确到秒

林继 VPS 知识 3,156 共写了208个字 (2011-01-22 15:21:07) 没有评论 打印 扫描二维码 百度已收录

本站所用倒计时代码:

  1. <br /><script language="javascript" type="text/javascript"> 
  2. var interval = 1000; 
  3. function ShowCountDown(year,month,day,divname) 
  4. { 
  5. var now = new Date(); 
  6. var endDate = new Date(year, month-1, day); 
  7. var leftTime=endDate.getTime()-now.getTime(); 
  8. var leftsecond = parseInt(leftTime/1000); 
  9. //var day1=parseInt(leftsecond/(24*60*60*6)); 
  10. var day1=Math.floor(leftsecond/(60*60*24)); 
  11. var hour=Math.floor((leftsecond-day1*24*60*60)/3600); 
  12. var minute=Math.floor((leftsecond-day1*24*60*60-hour*3600)/60); 
  13. var second=Math.floor(leftsecond-day1*24*60*60-hour*3600-minute*60); 
  14. var cc = document.getElementById(divname); 
  15. cc.innerHTML = "回校时间:"+year+"年"+month+"月"+day+"日<br />距今还有:"+day1+"天"+hour+"小时"+minute+"分"+second+"秒"; 
  16. } 
  17. window.setInterval(function(){ShowCountDown(2011,2,12,'divdown1');}, interval); 
  18. </script>

下雪代码:

  1. <script src="http://s1.wordpress.com/wp-content/plugins/snow/snowstorm.js" type="text/javascript"></script>

第一个:今天距离 2011年1月25日14时52分47秒 还有 2天 23时 27分 48.4秒

  1. <!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml" lang="gb2312">  
  3. <head>  
  4. <head>  
  5. <title> 演示实例:倒计时效果-精确到秒 </title>  
  6. <meta http-equiv="content-type" content="text/html; charset=gb2312" />  
  7. <meta name="keywords" content="" />  
  8. <meta name="description" content="" />  
  9. <script language="JavaScript">  
  10. <!-- //  
  11. var overDate = 1000 * 60 * 60 * 24 * 3;//截止时间:为当前计算机时间+3天  
  12. var NowTime = new Date();  
  13. //var EndTime= new Date(2009,01,11,0,0);   
  14. var EndTime= new Date(NowTime.getTime() + overDate);  
  15. var EndTimeMsg = EndTime.getFullYear() + "年";  
  16. EndTimeMsg = EndTimeMsg + (EndTime.getMonth()+1) + "月";  
  17. EndTimeMsg = EndTimeMsg + (EndTime.getDate()) + "日";  
  18. EndTimeMsg = EndTimeMsg + (EndTime.getHours()) + "时";  
  19. EndTimeMsg = EndTimeMsg + (EndTime.getMinutes()) + "分";  
  20. EndTimeMsg = EndTimeMsg + (EndTime.getSeconds()) + "秒";  
  21. function GetRTime(){  
  22.  
  23.     NowTime = new Date();  
  24.     var nMS=EndTime.getTime() - NowTime.getTime();  
  25.     var nD=Math.floor(nMS/(1000 * 60 * 60 * 24));  
  26.     var nH=Math.floor(nMS/(1000*60*60)) % 24;  
  27.     var nM=Math.floor(nMS/(1000*60)) % 60;  
  28.     var nS=Math.floor(nMS/1000) % 60;  
  29.     var nMS=Math.floor(nMS/100) % 10;  
  30.     if(nD>= 0){  
  31.         document.getElementById("RemainD").innerHTML=nD;  
  32.         document.getElementById("RemainH").innerHTML=nH;  
  33.         document.getElementById("RemainM").innerHTML=nM;  
  34.         document.getElementById("RemainS").innerHTML=nS + "." + nMS;  
  35.     }  
  36.     else {  
  37.         document.getElementById("CountMsg").innerHTML= EndTimeMsg +"已过!";  
  38.     }  
  39.     setTimeout("GetRTime()",100);  
  40. }  
  41. window.onload=function(){  
  42.     document.getElementById("EndTimeMsg").innerHTML = EndTimeMsg;  
  43.     GetRTime();  
  44. }  
  45. // -->  
  46. </script>  
  47. </head>  
  48. <body>  
  49. <div id="CountMsg">  
  50. 今天距离  
  51.     <strong id="EndTimeMsg">XX</strong>  
  52. 还有  
  53.     <strong id="RemainD">XX</strong>
  54.     <strong id="RemainH">XX</strong>
  55.     <strong id="RemainM">XX</strong>
  56.     <strong id="RemainS">XX</strong>
  57. </div>  
  58.  
  59. </body>  
  60. </html>

第二个:今天距离 2011年1月25日 还有 2天 23时 27分 48秒

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  5. <title>JS实现倒计时(时、分,秒)</title> 
  6. <script language="javascript" type="text/javascript"> 
  7. var interval = 1000; 
  8. function ShowCountDown(year,month,day,divname) 
  9. { 
  10. var now = new Date(); 
  11. var endDate = new Date(year, month-1, day); 
  12. var leftTime=endDate.getTime()-now.getTime(); 
  13. var leftsecond = parseInt(leftTime/1000); 
  14. //var day1=parseInt(leftsecond/(24*60*60*6)); 
  15. var day1=Math.floor(leftsecond/(60*60*24)); 
  16. var hour=Math.floor((leftsecond-day1*24*60*60)/3600); 
  17. var minute=Math.floor((leftsecond-day1*24*60*60-hour*3600)/60); 
  18. var second=Math.floor(leftsecond-day1*24*60*60-hour*3600-minute*60); 
  19. var cc = document.getElementById(divname); 
  20. cc.innerHTML = "脚本之家提示距离"+year+"年"+month+"月"+day+"日还有:"+day1+"天"+hour+"小时"+minute+"分"+second+"秒"; 
  21. } 
  22. window.setInterval(function(){ShowCountDown(2010,4,20,'divdown1');}, interval); 
  23. </script> 
  24. </head> 
  25. <body> 
  26. <div id="divdown1"></div> 
  27. </body> 
  28. </html>

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注

< >