Multiple JavaScript countdown dates?

元气小坏坏 提交于 2019-12-13 02:00:09

问题


So I currently have one date, but I want multiple and for them to have unique id's.

I can currently use <div id="countdown"></div> to put it in my html, but I want to do something like id="countdown1", id="countdown2", id="countdown3", etc.

To explain more in depth i'm trying to have several dates like this with unique id's for each one so I can put each individual one in the html.

var end = new Date(Date.UTC(2015, 10, 10));
var end = new Date(Date.UTC(2015, 10, 11));
var end = new Date(Date.UTC(2015, 10, 12));
var end = new Date(Date.UTC(2015, 10, 13));

This is my current script:

var end = new Date(Date.UTC(2015, 10, 10, 5));

var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;

function showRemaining() {
    var now = new Date();
    var distance = end - now;
    if (distance < 0) {

        clearInterval(timer);
        document.getElementById('countdown').innerHTML = 'EXPIRED!';

        return;
    }
    var days = Math.floor(distance / _day);
    var hours = Math.floor((distance % _day) / _hour);
    var minutes = Math.floor((distance % _hour) / _minute);
    var seconds = Math.floor((distance % _minute) / _second);

    document.getElementById('countdown').innerHTML = days + ' Day | ';
    document.getElementById('countdown').innerHTML += hours + ' Hours | ';
    document.getElementById('countdown').innerHTML += minutes + ' Minutes | ';
    document.getElementById('countdown').innerHTML += seconds + ' Seconds ';
}

timer = setInterval(showRemaining, 1000);

I tried a lot of different things and searched many times for a solution but have found nothing. Any help on this would be very appreciated.


回答1:


Use object, it is a cleaner solution.

function Timer(holder) {

        var controller = {
            holder: holder,
            end: null,
            intervalID:0,
            display: function () {
                var _second = 1000;
                var _minute = _second * 60;
                var _hour = _minute * 60;
                var _day = _hour * 24;

                var msg = "";

                var now = new Date();

                var distance = controller.end - now;
                if (distance < 0) {

                    clearInterval(controller.intervalID);
                    controller.holder.innerHTML = 'EXPIRED!';

                    return;
                }

                var days = Math.floor(distance / _day);
                var hours = Math.floor((distance % _day) / _hour);
                var minutes = Math.floor((distance % _hour) / _minute);
                var seconds = Math.floor((distance % _minute) / _second);
                controller.holder.innerHTML = days + ' Day | ' + hours + ' Hours | ' + minutes + ' Minutes | ' + seconds + ' Seconds ';

            }
        }

        this.countDown = function (end) {
            controller.end = end;
            controller.intervalID = setInterval(controller.display, 1000);
        }
    }

Working sample at https://jsfiddle.net/mLr571tj/

EDIT: Make sure you use the same date time format on initialization and in display function.




回答2:


Try scoping the variables to the function and returning it once it's called. I made an example for you: http://jsfiddle.net/wt9pb9r3/1/

I use setTimeoutto simulate the dates being initiated a few seconds apart.



来源:https://stackoverflow.com/questions/33600502/multiple-javascript-countdown-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!