比较全的JavaScript倒计时脚本
JavaScript倒计时在Web中用得非常广泛,比如常见的团购啊、还有什么值得期待的事情,都可以用到倒计时。现在举了四个例子,比如时间长的倒计时,小时倒计时,最简的倒计时,还有秒表等等,应该可以满足大部分需求。
1. 比较长时间的倒计时
<script type="text/javascript"> startclock(); var timerID = null; var timerRunning = false; function showtime() { Today = new Date(); var year = Today.getFullYear(); document.getElementById("next_yeat").innerHTML = year + 1; 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 = year - NowYear Monthleft = 12 - NowMonth - 1 Dateleft = 31 - NowDate if (Secondleft<0) { Secondleft=60+Secondleft; Minuteleft=Minuteleft-1; } if (Minuteleft<0) { Minuteleft=60+Minuteleft; Hourleft=Hourleft-1; } if (Hourleft<0) { Hourleft=24+Hourleft; Dateleft=Dateleft-1; } if (Dateleft<0) { Dateleft=31+Dateleft; Monthleft=Monthleft-1; } if (Monthleft<0) { Monthleft=12+Monthleft; Yearleft=Yearleft-1; } Temp=Yearleft+'年, '+Monthleft+'月, '+Dateleft+'天, '+Hourleft+'小时, '+Minuteleft+'分, '+Secondleft+'秒'; document.form1.left.value=Temp; timerID = setTimeout("showtime()",1000); timerRunning = true; } var timerID = null; var timerRunning = false; function stopclock () { if(timerRunning) clearTimeout(timerID); timerRunning = false; } function startclock () { stopclock(); showtime(); } // --> </script>
2. 小时倒计时(短时间倒计时)
<script type="text/javascript"> <!-- //一个小时,按秒计算,可以自己调整时间 var maxtime = 60*60 function CountDown() { if(maxtime>=0) { minutes = Math.floor(maxtime/60); seconds = Math.floor(maxtime%60); msg = "距离结束还有 "+minutes+" 分 "+seconds+" 秒"; document.all["timer"].innerHTML=msg; if(maxtime == 5*60) alert('注意,还有5分钟!'); --maxtime; } else { clearInterval(timer); alert("时间到,结束!"); } } timer = setInterval("CountDown()",1000); //--> </script>
转载:原文来自http://www.nowamagic.net/librarys/veda/detail/421