纯js倒计时效果(交流加群:452892873)(本群每天都更新学习资料)
<!doctype html> <html> <head> <meta charset="utf-8"> <title>纯js倒计时效果</title> </head> <body> <div id="time">2017/6/17 18:15:00</div> <script type="text/javascript"> var texts=document.getElementById('time').innerHTML;
function p(n){
return n<10?'0'+n:n;
}
function getMyTime(){
var startDate=new Date();
var endDate=new Date(texts);
var countDown=(endDate.getTime()-startDate.getTime())/1000;
var day=parseInt(countDown/(24*60*60));
var h=parseInt(countDown/(60*60)%24);
var m=parseInt(countDown/60%60);
var s=parseInt(countDown%60);
document.getElementById('time').innerHTML=p(day)+'天'+p(h)+'时'+p(m)+'分'+p(s)+'秒';
if(countDown<=0){
document.getElementById('time').innerHTML=00+'天'+00+'时'+00+'分'+00+'秒';
clearInterval(time);
};
}
time=setInterval(getMyTime,1000);
</script> </body> </html>