這裡給大家總結了一些比較常用的javascript實現的倒計時功能的代碼,非常的實用,有需要的小伙伴可以參考下。
這裡是一個JavaScript中顯示倒計時的方法,從10一秒一秒地往下減直到計時結束(即0),代碼如下:
?
1 2 3 4 5 6 7 8 9 10 runCount(10); function runCount(t){ if(t>0){ document.getElementById(‘shownum').innerHTML = t; t–; setTimeout(function(){runCount(t);},1000); }else{ document.getElementById(‘shownum').innerHTML = ‘倒計時結束!'; } }運行該js代碼前,需要先添加一個id為shownum的div。
js實現動態倒計時功能 * a是從後台接收到的時間戳,需轉換成毫秒單位
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 <div> 距明年還有: <span id='daya'></span>天 <span id='hoursa'></span>小時 <span id='minua'></span>分 <span id='secoa'></span>秒 </div> <script type="text/javascript"> /** * * copyright WE 2012.7 * js實現動態倒計時功能 * a是從後台接收到的時間戳,需轉換成毫秒單位 * */ var a=12345678; //以毫秒為單位 function fomtime() { a=a-1000; var b=new Date(); b.setTime(0); var c=new Date(); c.setTime(a); var day1=b.getDate(); //為方便調用,把天數、小時等單獨定義 var hours1=b.getHours(); var minu1=b.getMinutes(); var seco1=b.getSeconds(); var day2=c.getDate(); var hours2=c.getHours(); var minu2=c.getMinutes(); var seco2=c.getSeconds(); var day=day2-day1; var hours=hours2-hours1; var minu=minu2-minu1; var seco=seco2-seco1; document.getElementById('daya').innerHTML=day; document.getElementById('hoursa').innerHTML=hours; document.getElementById('minua').innerHTML=minu; document.getElementById('secoa').innerHTML=seco; setTimeout("fomtime()",1000); } fomtime(); </script>按天倒計時
HTML代碼1:
?
1 2 3 4 5 6 7 8 9 10 11 <Script Language="JavaScript"> <!-- Begin var timedate= new Date("January 14,2006"); var times="研究生考試"; var now = new Date(); var date = timedate.getTime() - now.getTime(); var time = Math.floor(date / (1000 * 60 * 60 * 24)); if (time >= 0) ; document.write("<li><font color=#DEDBDE>現在離2006年"+times+"還有: <font color=#ffffff><b>"+time +"</b></font> 天</font></li>"); // End --> </Script>HTML代碼2:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <script language="JavaScript" type="text/javascript"> function djs(){ var urodz= new Date("11/12/2008"); var now = new Date(); var num var ile = urodz.getTime() - now.getTime(); var dni = Math.floor(ile / (1000 * 60 * 60 * 24)); if (dni >1)num=dni+1 else if (dni == 1)num=2 else if (dni == 0)num=1 else num=0 document.write(num) } </script>距某某開幕式還有 [ ] 天
精確到秒的javascript倒計時代碼
HTML代碼:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 <form name="form1"> <div align="center" align="center"> <center>離2013年還有:<br> <input type="textarea" name="left" size="35" style="text-align: center"> </center> </div> </form> <script LANGUAGE="javascript"> startclock() var timerID = null; var timerRunning = false; function showtime() { Today = new Date(); var NowHour = Today.getHours(); var NowMinute = Today.getMinutes(); var NowMonth = Today.getMonth(); var NowDate = Today.getDate(); var NowYear = Today.getYear(); var NowSecond = Today.getSeconds(); if (NowYear <2000) NowYear=1900+NowYear; Today = null; Hourleft = 23 - NowHour Minuteleft = 59 - NowMinute Secondleft = 59 - NowSecond Yearleft = 2009 - NowYear Monthleft = 12 - NowMonth - 1 Dateleft = 31 - NowDate if (Secondleft<0)