JS倒数计时器与天:小时:分钟

JS Countdown timer with days:hours:minutes

本文关键字:小时 分钟 倒数 计时器 JS      更新时间:2023-09-26

>我找到了一个整洁的计时器,但我需要对其进行一些自定义才能为我的项目工作,我尝试更改 javascript 代码,以便直到周末才倒计时并从 ex: 6 天:23 小时:59 分钟,但我惨败了

我还想知道我怎么可能添加第三行,称为天

http://codepen.io/anon/pen/ZYEBjP

JS代码

var Clock = (function(){
    var exports = function(element) {
        this._element = element;
        var html = '';
        for (var i=0;i<6;i++) {
            html += '<span>&nbsp;</span>';
        }
        this._element.innerHTML = html;
        this._slots = this._element.getElementsByTagName('span');
        this._tick();
    };
    exports.prototype = {
        _tick:function() {
            var time = new Date();
            this._update(this._pad(time.getHours()) + this._pad(time.getMinutes()) + this._pad(time.getSeconds()));
            var self = this;
            setTimeout(function(){
                self._tick();
            },1000);
        },
        _pad:function(value) {
            return ('0' + value).slice(-2);
        },
        _update:function(timeString) {
            var i=0,l=this._slots.length,value,slot,now;
            for (;i<l;i++) {
                value = timeString.charAt(i);
                slot = this._slots[i];
                now = slot.dataset.now;
                if (!now) {
                    slot.dataset.now = value;
                    slot.dataset.old = value;
                    continue;
                }
                if (now !== value) {
                    this._flip(slot,value);
                }
            }
        },
        _flip:function(slot,value) {
            // setup new state
            slot.classList.remove('flip');
            slot.dataset.old = slot.dataset.now;
            slot.dataset.now = value;
            // force dom reflow
            slot.offsetLeft;
            // start flippin
            slot.classList.add('flip');
        }
    };
    return exports;
}());
var i=0,clocks = document.querySelectorAll('.clock'),l=clocks.length;
for (;i<l;i++) {
    new Clock(clocks[i]);
}

我会创建一个助手:

var TimeHelper = function(days, hours, minutes, callback) {
    this.subtractMinute = function() {
        minutes = (minutes + 60 - 1) % 60;
        if (minutes === 0) {
            hours = (hours + 60 - 1) % 60;
            if (hours === 0) {
                days = (days + 24 - 1) % 24;
                if (days === 0) {
                    days = 24;
                    hours = 0;
                    minutes = 0;
                }
            }
        }
        callback(days, hours, minutes);
    }
}
function refreshUI(days, hours, minutes) {
    //refresh my ui elements
}
var timeHelper = new TimeHelper(24, 0, 0);

然后你可以每分钟调用timeHelper.subtractMinute一次/分钟