js倒計時的實現html
使用js實現倒計時函數
分兩個部分,html頁面內容,和js函數spa
1.htmlhtm
<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>ip
2.js函數get
<!-- 倒計時 -->
<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);string
</SCRIPT>io