一个页面多个倒计时的封装

          


<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
window.onload=function(){
var endtimes=new Array();
/*页面上设置多个定时器*/
endtimes[0]="05/16/2015 15:03:00";
endtimes[1]="06/01/2015 9:04:23";
endtimes[2]="04/25/2015 15:04:11";
endtimes[3]="07/01/2015 9:04:05";
var timer=null;
DownCount();
setInterval( DownCount,1000);
function DownCount(){
var nowtimes=new Date();
for(var i=0;i<4;i++)
{
var theDay=new Date(endtimes[i]);
t = Math.floor( ( theDay - nowtimes ) / 1000 );
if(t<=0)
{
document.getElementById("times"+i).innerHTML = "00Day 00h 00m 00s ";
clearInterval( timer );
}
else

if(Math.floor(t/86400)<=1)
{
str = toTwo(Math.floor(t/86400))+'Day '+toTwo(Math.floor(t%86400/3600))+'h '+toTwo(Math.floor(t%86400%3600/60))+'m '+toTwo(t%60)+'s ';
document.getElementById("times"+i).innerHTML = str;

}
else
{
str = toTwo(Math.floor(t/86400))+'Days '+toTwo(Math.floor(t%86400/3600))+'h '+toTwo(Math.floor(t%86400%3600/60))+'m '+toTwo(t%60)+'s ';
document.getElementById("times"+i).innerHTML = str;
}
}


};

/*当出现一个数字的时候添加一个0例如04秒*/
function toTwo ( n ) {
return n < 10 ? '0' + n : '' + n;
};

}
</script>

</head>

<body>
<span id='times0'></span>
<br/>
<span id='times1'></span>
<br/>
<span id='times2'></span>
<br/>
<span id='times3'></span>
</body>
</html>
原文地址:https://www.cnblogs.com/mgqworks/p/7651843.html