js 定时器实现倒计时
var startDate = new Date('2017-9-6').getTime();//结束时间
function countDown(){
var nowDate = new Date().getTime();//当前实时时间
if(nowDate>startDate){//已结束,求出结束了多久
var alreadyTime = Math.floor((nowDate-startDate)/1000/60);//已经过了多少分钟
console.log('已经过了'+alreadyTime+'分钟');
}else{//时间还没到
var remainTime = startDate - nowDate;
var day = Math.floor(remainTime/1000/60/60/24)
var hour=Math.floor(remainTime /1000/60/60%24);
var min=Math.floor(remainTime /1000/60%60);
var sec=Math.floor(remainTime /1000%60);
if (hour < 10) {
hour = "0" + hour;
}
if (min < 10) {
min = "0" + min;
}
if (sec < 10) {
sec = "0" + sec;
}
console.log('距开始时间还有:'+hour+'天'+hour+'小时'+min+'分'+sec+'秒')
}
}
//启用定时器
var time = setInterval(countDown,1000);
//清空定时器
clearInterval(time);
time = null;