Javascript.RU

Создать новую тему Ответ
 
Опции темы Искать в теме
  #1 (permalink)  
Старый 17.03.2019, 18:21
Новичок на форуме
Отправить личное сообщение для adwebzoom Посмотреть профиль Найти все сообщения от adwebzoom
 
Регистрация: 09.04.2018
Сообщений: 7

Как зациклить счетчик
Доброго времени!
Подскажите как зациклить таймер, чтобы когда заканчивался отсчет начинался заново.

$(function () {
    $('.psproductcountdown').each(function(){
        var to = parseInt($(this).data('to'));
        if (!to) {
            return true;
        }

        var labels = pspc_labels,
            template = _.template(pspc_countdown_tpl),
            $pspc = $(this).find('.pspc-main');
        if (pspc_show_weeks) {
            var currDate = '00:00:00:00:00';
            var nextDate = '00:00:00:00:00';
        } else {
            var currDate = '00:00:00:00';
            var nextDate = '00:00:00:00';
        }

        // Parse countdown string to an object
        function strfobj(str) {
            var pieces = str.split(':');
            var obj = {};
            labels.forEach(function(label, i) {
                obj[label] = pieces[i]
            });
            return obj;
        }
        // Return the time components that diffs
        function diff(obj1, obj2) {
            var diff = [];
            labels.forEach(function(key) {
                if (obj1[key] !== obj2[key]) {
                    diff.push(key);
                }
            });
            return diff;
        }
        // Build the layout
        var initData = strfobj(currDate);
        labels.forEach(function(label, i) {
            $pspc.append(template({
                curr: initData[label],
                next: initData[label],
                label: label,
                label_lang: pspc_labels_lang[label]
            }));
        });
        // Starts the countdown
        $pspc.countdown(to, function(event) {
            var data;
            if (pspc_show_weeks)
                var newDate = event.strftime('%w:%d:%H:%M:%S');
            else
                var newDate = event.strftime('%D:%H:%M:%S');

            if (newDate !== nextDate) {
                currDate = nextDate;
                nextDate = newDate;
                // Setup the data
                data = {
                    'curr': strfobj(currDate),
                    'next': strfobj(nextDate)
                };
                // Apply the new values to each node that changed
                diff(data.curr, data.next).forEach(function(label) {
                    var selector = '.%s'.replace(/%s/, label),
                        $node = $pspc.find(selector);
                    // Update the node
                    $node.removeClass('flip hidden');
                    $node.find('.curr').text(data.curr[label]);
                    $node.find('.next').text(data.next[label]);
                    // Wait for a repaint to then flip
                    _.delay(function($node) {
                        $node.addClass('flip');
                    }, 50, $node);
                });
            }
        });
    });
});

var pspc_countdown_tpl = '' +
        '<div class="time <%= label %>">' +
            '<span class="count curr top"><%= curr %></span>' +
            '<span class="count next top"><%= next %></span>' +
            '<span class="count next bottom"><%= next %></span>' +
            '<span class="count curr bottom"><%= curr %></span>' +
            '<span class="label"><%= label_lang.length < 6 ? label_lang : label_lang.substr(0, 3)  %></span>' +
    '</div>';
Ответить с цитированием
Ответ


Опции темы Искать в теме
Искать в теме:

Расширенный поиск


Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Как зациклить таймер? artg Общие вопросы Javascript 0 31.01.2018 21:46
Как правильно реализовать такой функционал? Julian Общие вопросы Javascript 3 16.01.2015 12:34
Смена картинок. Как зациклить при нажатии? Aggao Общие вопросы Javascript 6 02.12.2014 16:38
Мотоциклисты есть? l-liava-l Оффтопик 13 04.07.2014 13:35
Как зациклить карусель dolte Events/DOM/Window 1 08.04.2014 16:38