标签:
js倒计时的实现
使用js实现倒计时
分两个部分,html页面内容,和js函数
1.html
<div id="times">
<div id="dayId">NaN</div><i>天</i>
<div id="hourId">NaN</div><i>小时</i>
<div id="minuteId">NaN</div><i>分钟</i>
<div id="secondId">NaN</div><i>秒</i>
</div>
2.js函数
<!-- 倒计时 -->
<SCRIPT LANGUAGE="JavaScript">
function _fresh()
{
//获取结束时间
var endDate=document.getElementById("panicBuyingEndDate").value;
if(endDate==null||endDate==""){
document.getElementById("times").innerHTML="倒计时结束";
}else{
var year=endDate.substring(0,4);
var month=parseInt(endDate.substring(5,7))-1;
var day=endDate.substring(8,10);
var hour=endDate.substring(11,13);
var min=endDate.substring(14,16);
var second=endDate.substring(17,19);
var endtime =new Date(year,month,day,hour,min,second);
var nowtime = new Date();//获取当前时间
var leftsecond=parseInt((endtime.getTime()-nowtime.getTime())/1000);
__d=parseInt(leftsecond/3600/24);
__h=parseInt((leftsecond/3600)%24);
__m=parseInt((leftsecond/60)%60);
__s=parseInt(leftsecond%60);
document.getElementById("dayId").innerHTML=__d;
document.getElementById("hourId").innerHTML=__h;
document.getElementById("minuteId").innerHTML=__m;
document.getElementById("secondId").innerHTML=__s;
if(leftsecond<=0){
document.getElementById("times").innerHTML="倒计时已结束";
clearInterval(sh);
}
}
}
_fresh()
var sh;
sh=setInterval(_fresh,1000);
</SCRIPT>
标签:
原文地址:http://my.oschina.net/u/1450300/blog/525278