接收的参数end是必须传的,格式是/分隔的日期字符串,start是可选的,不传就是从现在开始倒计时,callback也是可选的,到倒计时接收时执行自定义的函数。
countdown({ ‘end‘:‘2015/9/1 17:12:00‘, ‘callback‘:function () { document.getElementById(‘countdown‘).innerHTML = ‘the end!‘; } });
(function (){ function countdown(config){ var startDate = config.start ? new Date(config.start) : new Date(); var endDate = new Date(config.end); var id = config.id || ‘countdown‘; var time = (endDate - startDate)/1000; if(time < 0){ 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); setTimeout(function (){ document.getElementById(id).innerHTML = day+‘ day ‘+hour+‘:‘+minute+‘:‘+second; countdown(config); }, 1000); } window.countdown = countdown; })();
时间: 2024-07-30 13:42:16