0

День добрый.

Есть такой код

$(document).ready(function () {
    function CountdownTimer(elm, tl, mes) {
        this.initialize.apply(this, arguments);
    }
    CountdownTimer.prototype = {
        initialize: function (elm, tl, mes) {
            this.elem = document.getElementById(elm);
            this.tl = tl;
            this.mes = mes;
        },
        countDown: function () {
            var timer = '';
            //  var today=new Date();
            var today = (new Date()).getTime() + 60 * 1000;
            var day = Math.floor((this.tl - today) / (24 * 60 * 60 * 1000));
            var hour = Math.floor(((this.tl - today) % (24 * 60 * 60 * 1000)) / (60 * 60 * 1000));
            var min = Math.floor(((this.tl - today) % (24 * 60 * 60 * 1000)) / (60 * 1000)) % 60;
            var sec = Math.floor(((this.tl - today) % (24 * 60 * 60 * 1000)) / 1000) % 60 % 60;
            var me = this;

            if ((this.tl - today) > 0) {
                timer += '<span class="number-wrapper"><div class="line"></div><div class="caption">Дни</div><span class="number day">' + day + '</span></span>';
                timer += '<span class="number-wrapper"><div class="line"></div><div class="caption">Часы</div><span class="number hour">' + hour + '</span></span>';
                timer += '<span class="number-wrapper"><div class="line"></div><div class="caption">Минуты</div><span class="number min">' + this.addZero(min) + '</span></span><span class="number-wrapper"><div class="line"></div><div class="caption">Секунды</div><span class="number sec">' + this.addZero(sec) + '</span></span>';
                this.elem.innerHTML = timer;
                tid = setTimeout(function () {
                    me.countDown();
                }, 10);
            } else {
                this.elem.innerHTML = this.mes;
                return;
            }
        },
        addZero: function (num) {
            return ('0' + num).slice(-2);
        }
    }

    function CDT() {

        // Set countdown limit
        var tl = new Date();

        alert(tl);

        // You can add time's up message here
        var timer = new CountdownTimer('CDT', tl, '<span class="number-wrapper"><div class="line"></div><span class="number end">Время истекло</span></span>');
        timer.countDown();
    }

    function CDT2() {

        // Set countdown limit
        var tl = new Date('2014/03/30 00:00:00');

        // You can add time's up message here
        var timer = new CountdownTimer('CDT2', tl, '<span class="number-wrapper"><div class="line"></div><span class="number end">Время истекло</span></span>');
        timer.countDown();
    }
    window.onload = function () {
        CDT();
        CDT2();
    }

    function CountdownTimer(elm, tl, mes) {
        this.initialize.apply(this, arguments);
    }

});

Подскажите пожалуйста как сделать так, чтобы он считал оставшееся время до конца суток? Т.е. каждый день бы обнулялся и начинал сначала 24 часа отсчитывать?

1 ответ 1

1

Получить завтрашний день можно таким образом

var t = new Date();
    t.setDate(t.getDate() + 1);
    var tl = new Date(t.getFullYear() + '/' + (t.getMonth() + 1) + '/' + t.getDate() + ' 00:00:00');

Рабочий пример с вашим кодом на jsfiddle (отсчет до конца текущего дня в функции CDT())

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.