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;

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/8834.html

(0)
上一篇 2021年7月18日 22:21
下一篇 2021年7月18日 22:21

相关推荐

发表回复

登录后才能评论