当前位置: 代码迷 >> Web前端 >> count down 记时
  详细解决方案

count down 记时

热度:500   发布时间:2012-10-09 10:21:45.0
count down 倒计时
<!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> new document </title>
  <meta name="generator" content="editplus" />
  <meta name="author" content="" />
  <meta name="keywords" content="" />
  <meta name="description" content="" />
</head>

<body>
  <ul>
<li id="day">0</li>
<li id="hour">0</li>
<li id="min">0</li>
<li id="sec">0</li>
<li id="nsec">0</li>
  </ul>
</body>
<script language="JavaScript" type="text/javascript">
<!--
function Timer(opt){
option = opt||{};
this.startDate=option.star||new Date("2011/5/11 07:00:00");
//this.endDate=option.star||new Date("2011/8/6 07:00:00");
                this.endDate=option.end||new Date("2011/8/6 07:00:00");
this.nDay=document.getElementById("day");
this.nHour=document.getElementById("hour");
this.nMin=document.getElementById("min");
this.nSec=document.getElementById("sec");
this.nNsec=document.getElementById("nsec");
this.count();
}
Timer.prototype = {
leftPad: function(num){
if(num.toString().length==1){
return "0"+ num;
}
return num;
}
,count: function(){
var self = this;
var nowDate = new Date();
if(nowDate.getTime()-this.startDate.getTime() > 0){
//way 1. I write
var totalns = this.endDate.getTime() - nowDate.getTime();
var nDayn=Math.floor(totalns/(1000*60*60*24));
var nHourn=Math.floor(totalns%(1000*60*60*24)/(1000*60*60));
//对余数(毫秒数)除以一小时的毫秒数
var nMinn=Math.floor(totalns%(1000*60*60*24)%(1000*60*60)/(1000*60));
//对余数(毫秒数)除以一分钟的毫秒数
var nSecn=Math.floor(totalns%(1000*60*60*24)%(1000*60*60)%(1000*60)/1000);
var nNsecn=Math.floor(totalns%(1000*60*60*24)%(1000*60*60)%(1000*60)%1000);
//way 2. Someone write. good code
/*var nDayn=Math.floor(totalns/(1000*60*60*24));
var nHourn=Math.floor(totalns/(1000*60*60))%24;
//对余数(所有小时)除以24求余
var nMinn=Math.floor(totalns/(1000*60))%60;
var nSecn=Math.floor(totalns/(1000))%60;
var nNsecn=Math.floor(totalns%1000);*/
if(nowDate>0){
this.nDay.innerHTML=nDayn;
this.nHour.innerHTML=nHourn;
this.nMin.innerHTML=nMinn;
this.nSec.innerHTML=nSecn;
this.nNsec.innerHTML=nNsecn;
}
}

setTimeout(function(){self.count();},1);
}
}
new Timer({});
//Timer({})
//-->
</script>
</html>
  相关解决方案