<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ment</title>
<script src="time.js"></script>
</head>
<body>
<div id="countdown"></div>
<script>// 引用倒计时
countdown({
'end':['2016/6/27 23:18:00' , '2016/6/30 24:00:00' , '2016/7/01 24:00:00'],
'callback':function () {
document.getElementById('countdown').innerHTML = 'the end!';
// $('.login_award_time').remove();
}
});
</script>
</body>
</html>
兼容 safari 的写法是 new Date() 如果时间大于当前时间 会报错 invalid Date 处理方法是 可以 new Date('2018','09','12','12','32','00')
(function (){
function countdown(config){
var startDate = config.start ? new Date(config.start) : new Date();
console.log('startgettime'+startDate.getTime());
for(var i=0;i<config.end.length;i++){
/*2016/06/30 24:00:00 */
/*处理safari new Date() 超出当前时间的 invalid date 报错 经测试 safari 可以接纳 new Date('2019','10','21','22','00',''09) config.end[i].substring(5,7)-1 是因为月份的原因这个方式*/
var nums = new Date(config.end[i].substring(0,4),config.end[i].substring(5,7)-1,config.end[i].substring(8,10),config.end[i].substring(11,13),config.end[i].substring(14,16))
alert('nums'+nums+'now day' + new Date());
var num =(nums - new Date(startDate ))/1000;
if (num > 0 ){
console.log('config.endtime',config.endtime)
break;
}
}
var endDate = nums;
var id = config.id || 'countdown';
var time = (endDate - startDate)/1000;
console.log(time)
if(time < 0 || isNaN(time)){
if(config.callback){
config.callback();
}
return;
}
var day = parseInt(time/86400, 10);
var hour = parseInt(time%86400/60/60, 10);
var minute = parseInt(time%86400%3600/60, 10);
var second = parseInt(time%86400%3600%60, 10);
var mayday = day > 0?day+'天':'';
var mayhour = hour<10?'0'+hour:hour;
var mayminute = minute<10?'0'+minute : minute;
var maysecond = second<10?'0'+second : second;
setTimeout(function (){
document.getElementById(id).innerHTML = mayday+mayhour+':'+mayminute+':'+maysecond;
countdown(config);
}, 1000);
}
window.countdown = countdown;
})();