js html 頁面倒計時 精確到秒,jshtml頁面倒計時
<!doctype html><html><head> <meta charset="utf-8"> </head><body><DIV id="CountMsg" class="HotDate"> <span id="t_d">00天</span> <span id="t_h">00時</span> <span id="t_m">00分</span> <span id="t_s">00秒</span></DIV><script type="text/javascript"> function getRTime(){ var EndTime= new Date('2014/10/23 10:00:00'); //截止時間 前端路上 http://www.51xuediannao.com/qd63/ var NowTime = new Date(); var t =EndTime.getTime() - NowTime.getTime(); /*var d=Math.floor(t/1000/60/60/24); t-=d*(1000*60*60*24); var h=Math.floor(t/1000/60/60); t-=h*60*60*1000; var m=Math.floor(t/1000/60); t-=m*60*1000; var s=Math.floor(t/1000);*/ var d=Math.floor(t/1000/60/60/24); var h=Math.floor(t/1000/60/60%24); var m=Math.floor(t/1000/60%60); var s=Math.floor(t/1000%60); document.getElementById("t_d").innerHTML = d + "天"; document.getElementById("t_h").innerHTML = h + "時"; document.getElementById("t_m").innerHTML = m + "分"; document.getElementById("t_s").innerHTML = s + "秒"; } setInterval(getRTime,1000); </script> </body></html>
Js倒計時代碼精確到毫秒
setTimeout("GetRTime()", 100); 改為setTimeout("GetRTime()", 20); 就是≈50毫秒了,1s = 1000ms,1000 ÷ 50 = 20,然後由於javascript的單線程方式,setTimeout並不一定會在20ms時執行,所以只能說≈了
網頁倒計時代碼,精確到秒
指令碼說明:
第一步:把如下代碼加入<body>地區中
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function getTime() {
now = new Date();
y2k = new Date("Jan 1 2005 14:00:00");
days = (y2k - now) / 1000 / 60 / 60 / 24;
daysRound = Math.floor(days);
hours = (y2k - now) / 1000 / 60 / 60 - (24 * daysRound);
hoursRound = Math.floor(hours);
minutes = (y2k - now) / 1000 /60 - (24 * 60 * daysRound) - (60 * hoursRound);
minutesRound = Math.floor(minutes);
seconds = (y2k - now) / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound);
secondsRound = Math.round(seconds);
sec = (secondsRound == 1) ? " second." : " seconds.";
min = (minutesRound == 1) ? " minute" : " minutes, ";
hr = (hoursRound == 1) ? " hour" : " hours, ";
dy = (daysRound == 1) ? " day" : " days, "
document.timeForm.input1.value = "剩餘時間: " + daysRound + dy + hoursRound + hr + minutesRound + min + secondsRound + sec;
newtime = window.setTimeout("getTime();", 1000);
}
// End -->
</script>
距離: Jan 1, 2005 2:00:00 p.m.
<form name=timeForm>
<input type=text name=input1 size=70 border-style="none" style="border-bottom: 0px solid; border-left: 0px solid;border-right: 0px solid;border-top: 0px solid;font:12px arial, helvetica,sans-serif">
</form>
第二步:把<body>中的內容改為:
<body bgcolor="......餘下全文>>