特定日期的一页上有多个JS计数计时器

时间:2014-09-19 15:23:17

标签: javascript jquery html timer countdown

我有一个从特定日期起计数的计时器。 http://jsfiddle.net/eyntL8go/

我现在正在尝试调整它,以便我可以在页面上有多个计时器。我写了下面的代码,但它不起作用。任何人都可以看到错误吗?

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);
});

1 个答案:

答案 0 :(得分:0)

您可以将.innerHTML替换为.html()

DEMO