用于多个“时钟”的倒计时JavaScript

时间:2014-01-27 11:16:01

标签: javascript jquery countdown

我需要在一个页面上创建多个倒计时时钟。目前我只有一个,但我想使脚本变得更好和紧凑,所以我想对日期使用相同的计算,但只是简单地提取不同的值。

一个时钟倒计时到英联邦游戏的开始。我需要另一个倒计时结束仪式。

脚本:

// set the date we're counting down to
var target_date_opening = new Date("Jul 23, 2014").getTime();
var target_date_closing = new Date("Aug 23, 2014").getTime();

// variables for time units
var days, hours, minutes, seconds;

// get tag element
var countdownOpening = document.getElementById("countdownOpening");
var countdownClosing = document.getElementById("countdownClosing");

// update the tag with id "countdownOpening" every 1 second
setInterval(function () {
    // find the amount of "seconds" between now and target
    var current_date = new Date().getTime();
    var seconds_left = (target_date_opening - current_date) / 1000;

    // 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
    countdownOpening.innerHTML = days + " days " + hours + " hours "
    + minutes + " minutes " + seconds + " seconds";  

}, 1000);

您可以看到我创建了新变量来区分目标日期(target_date_closing

我将innerHTML的ID作为countdownClosing。

我的问题是为setInterval计算target_date_closing函数并输出countdownClosing而不再复制脚本。

我不确定是否需要使用个别变量来存储minutesdayshours& seconds变量。

2 个答案:

答案 0 :(得分:1)

尝试

// set the date we're counting down to
var target_date_opening = new Date("Jul 23, 2014").getTime();
var target_date_closing = new Date("Aug 23, 2014").getTime();

// variables for time units
var days, hours, minutes, seconds;

// get tag element
var countdownOpening = document.getElementById("countdownOpening");
var countdownClosing = document.getElementById("countdownClosing");

// update the tag with id "countdownOpening" every 1 second
setInterval(function () {
    print(countdownOpening, target_date_opening)
    print(countdownClosing, target_date_closing)
}, 1000);

function print(el, time) {
    // find the amount of "seconds" between now and target
    var current_date = new Date().getTime();
    var seconds_left = (time - current_date) / 1000;

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

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

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

    // format countdown string + set tag value
    el.innerHTML = days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds";
}

演示:Fiddle

答案 1 :(得分:0)

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script language="JavaScript">
        // set the date we're counting down to
        var target_date_opening = new Date("2014-07-23").getTime();
        var target_date_closing = new Date("Aug 23, 2014").getTime();

        // variables for time units
        var days, hours, minutes, seconds;

        var showCountdown = function(elementID, dt) {

            var elem = document.getElementById(elementID);

            // update the tag with id "countdownOpening" every 1 second
            setInterval(function () {
                // find the amount of "seconds" between now and target
                var current_date = new Date().getTime();
                var seconds_left = (dt - current_date) / 1000;

                // 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
                elem.innerHTML = days + " days " + hours + " hours "
                        + minutes + " minutes " + seconds + " seconds";

            }, 1000);

        };
    </script>

</head>
<body>
<div id="countdownOpening"></div>
<div id="countdownClosing"></div>
     <script>
        showCountdown('countdownOpening', target_date_opening);
        showCountdown('countdownClosing', countdownClosing);

    </script>
</body>
</html>