JavaScript倒计时

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
/*
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;

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:C++ 求两日期间相隔天数

下一篇:libssh2 执行命令