JavaScript倒計時
/
var countdown = new CountDown(
document.getElementById('countdown_wrapper'),
new Date(2015, 8, 27, 0, 0)
);
countdown.run();/
var CountDown = function(wrapper, endDate) {
// init
this.wrapper = wrapper;
this.timerRunning = false;
this.endDate = endDate;
this.template = '{days} DAYS {hours} HOURS {mins} MINS {secs} SECS';
}
CountDown.prototype.showtime = function(){
var now = new Date();
var leftTime = this.endDate.getTime() - now.getTime();
var leftsecond = parseInt(leftTime / 1000);
var day1 = Math.floor(leftsecond / (60 60 24));
var hour1 = Math.floor((leftsecond - day1 24 60 60) / 3600);
var hour = Math.floor((leftsecond - 60 60) / 3600);
if (hour < 0) {
hour = 0;
}
if (day1 < 0) {
hour = hour1
}
var minute = Math.floor((leftsecond - day1 24 60 60 - hour1 3600) / 60);
var second = Math.floor(leftsecond - day1 24 60 60 - hour1 3600 - minute * 60);
var html = '';
if (leftTime > 0) {
html = this.template;
html = html.replace('{days}', day1);
html = html.replace('{hours}', hour1);
html = html.replace('{mins}', minute);
html = html.replace('{secs}', second);
this.wrapper.innerHTML = html;
} else {
html = this.template;
html = html.replace('{days}', 0);
html = html.replace('{hours}', 0);
html = html.replace('{mins}', 0);
html = html.replace('{secs}', 0);
this.wrapper.innerHTML = html;
}
this.timerRunning = true;
}
CountDown.prototype.run = function(){
var _this = this;
CountDown_timer = setTimeout(function() {
_this.showtime();
CountDown_timer = setTimeout(arguments.callee, 1000);
_this.timerRunning = true;
}, 1000);
}
CountDown.prototype.stop = function(){
if(timerRunning)
clearTimeout(CountDown_timer);
this.timerRunning = false;
}
CountDown.prototype.constructor = CountDown;</pre>
本文由用戶 m45y 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!