0

js实现倒计时

  1. <html>  
  2.     <head>  
  3.         <title>倒计时</title>  
  4.         <!–以下为css样式–>  
  5.         <style type“text/css”>  
  6.             .daojishi h2  
  7.             {   
  8.                 font-family:Helvetica, Microsoft YaHei, Arial, sans-serif;   
  9.                 font-size:14px;  
  10.                 margin-bottom:5px;   
  11.                 color:#151515;  
  12.             }  
  13.             .daojishi #timer  
  14.             {  
  15.                 font-family:Helvetica, Microsoft YaHei, Arial, sans-serif;   
  16.                 font-size:14px;  
  17.                 color:#151515;  
  18.                 font-weight:bold;  
  19.             }  
  20.         </style>  
  21.         <script type = “text/javascript” src = “timer.js”>  
  22.         </script>  
  23.     </head>  
  24.     <body onload = “timer()”>  
  25.         <div class = “daojishi”>  
  26.             <h2>剩余时间为:</h2>  
  27.             <div id = “timer”>  
  28.             </div>  
  29.         </div>  
  30.     </body>  
  31. </html>  

timer.js

[javascript] view plaincopy
  1. function timer()  
  2.             {  
  3.                 var ts = (new Date(2018, 11, 11, 9, 0, 0)) – (new Date());//计算剩余的毫秒数  
  4.                 var dd = parseInt(ts / 1000 / 60 / 60 / 24, 10);//计算剩余的天数  
  5.                 var hh = parseInt(ts / 1000 / 60 / 60 % 24, 10);//计算剩余的小时数  
  6.                 var mm = parseInt(ts / 1000 / 60 % 60, 10);//计算剩余的分钟数  
  7.                 var ss = parseInt(ts / 1000 % 60, 10);//计算剩余的秒数  
  8.                 dd = checkTime(dd);  
  9.                 hh = checkTime(hh);  
  10.                 mm = checkTime(mm);  
  11.                 ss = checkTime(ss);  
  12.                 document.getElementById(“timer”).innerHTML = dd + “天” + hh + “时” + mm + “分” + ss + “秒”;  
  13.                 setInterval(“timer()”,1000);  
  14.             }  
  15.             function checkTime(i)    
  16.             {    
  17.                if (i < 10) {    
  18.                    i = “0” + i;    
  19.                 }    
  20.                return i;    
  21.             }    

一个页面中有多个倒计时

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” ” http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=” http://www.w3.org/1999/xhtml”>
<head>
<title>Timer – by Vilic JavaScriptQQ交流群56484577</title>
</head>
<body>
<div id=”timer1″></div>
<div id=”timer2″></div>
<div id=”timer3″></div>
</body>
<script type=”text/javascript”>

var addTimer = function () {
var list = [],
interval;

return function (id, time) {
if (!interval)
interval = setInterval(go, 1000);
list.push({ ele: document.getElementById(id), time: time });
}

function go() {
for (var i = 0; i < list.length; i++) {
list[i].ele.innerHTML = getTimerString(list[i].time ? list[i].time -= 1 : 0);
if (!list[i].time)
list.splice(i–, 1);
}
}

function getTimerString(time) {
var not0 = !!time,
d = Math.floor(time / 86400),
h = Math.floor((time %= 86400) / 3600),
m = Math.floor((time %= 3600) / 60),
s = time % 60;
if (not0)
return “还有” + d + “天” + h + “小时” + m + “分” + s + “秒”;
else return “时间到”;
}
} ();

addTimer(“timer1”, 12);
addTimer(“timer2”, 10);
addTimer(“timer3”, 13);
</script>
</html>

天边的星星