简体   繁体   中英

Multiple JS countup timers on one page from specific dates

I have got a countup timer that counts up from a certain date. http://jsfiddle.net/eyntL8go/

I am now trying to adapt this so that I can have multiple timers on a page. I have written the below code, but it doesn't work. Can anyone see the error?

HTML:

<div class="countup">
    <div id="targetdate" style="display: none">Thu Jan 01 1970 00:44:39 +0000</div>
    <div id="timer"></div>
</div>
<div class="countup">
    <div id="targetdate" style="display: none">Thu Jan 01 1970 00:44:49 +0000</div>
    <div id="timer"></div>
</div>

JS:

$('.countup').each(function() {
    var targetdate = $(this).children('#targetdate').innerHTML;
    var seconds_left = new Date(targetdate).getTime();
    var timer = $(this).children('#timer').innerHTML;
    var minutes, seconds;

    seconds_left = seconds_left / 1000;

    var countdownrefesh = setInterval(function () {
        // Add one to seconds
        seconds_left = seconds_left + 1;

        // do some time calculations
        days = parseInt(seconds_left / 86400);
        seconds_left = seconds_left % 86400;

        hours = parseInt(seconds_left / 3600);
        seconds_left = seconds_left % 3600;

        minutes = parseInt(seconds_left / 60);
        seconds = parseInt(seconds_left % 60);

        // format countdown string + set tag value
        timer = hours + "h:" + minutes + "m:" + seconds + "s";  

    }, 1000);
});

you can replace .innerHTML with .html()

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM