1 <html> 2 <head> 3<meta charset="UTF-8"> 4<title>距离活动结束还有 xx天xx时xx分xx秒</title> 5<script type="text/javascript"> 6 function countTime() {7 //获取当前时间 8 var date = new Date(); 9 var now = date.getTime(); 10 //设置截止时间 11 var str="/3/17 00:00:00";12 var endDate = new Date(str); 13 var end = endDate.getTime(); 14 15 //时间差 16 var leftTime = end-now; 17 //定义变量 d,h,m,s保存倒计时的时间 18 var d,h,m,s; 19 if (leftTime>=0) {20 d = Math.floor(leftTime/1000/60/60/24); 21 h = Math.floor(leftTime/1000/60/60%24); 22 m = Math.floor(leftTime/1000/60%60); 23 s = Math.floor(leftTime/1000%60); 24 }25 //将0-9的数字前面加上0,例1变为0126 d = checkTime(d);27 h = checkTime(h);28 m = checkTime(m);29 s = checkTime(s);30 function checkTime(i){31 if (i<10) {32 i = "0"+i;33 }34 return i;35 }36 //将倒计时赋值到div中 37 document.getElementById("_d").innerHTML = d+"天"; 38 document.getElementById("_h").innerHTML = h+"时"; 39 document.getElementById("_m").innerHTML = m+"分"; 40 document.getElementById("_s").innerHTML = s+"秒"; 41 //递归每秒调用countTime方法,显示动态时间效果 42 setTimeout(countTime,1000); 43 44 } 45</script> 46 </head > 47 <body onload="countTime()" > 48<div> 49 <span id="_d">00</span> 50 <span id="_h">00</span> 51 <span id="_m">00</span> 52 <span id="_s">00</span> 53</div> 54 </body> 55 </html>
100字范文,内容丰富有趣,生活中的好帮手!