同一页面上的多个Javascript倒计时

时间:2013-05-23 16:59:11

标签: javascript countdown

您好我正在尝试将几个javascript倒计时添加到单个html页面。我在下面包含了.js文件。现在我的页面只显示第一个倒计时。

function cdtd () {
    var end = new Date("December 25, 2013 00:01:00");
    var start = new Date();
    var timeDiff = end.getTime() - start.getTime();
    if (timeDiff <= 0) {
        clearTimeout (timer);
        document.write("Deal Ended");
    }
    var seconds = Math.floor(timeDiff / 1000);
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);
    var days = Math.floor(hours / 24);
    hours %= 24;
    minutes %= 60;
    seconds %=60;

    document.getElementById("daysBox").innerHTML = days;
    document.getElementById("hoursBox").innerHTML = hours;
    document.getElementById("minsBox").innerHTML = minutes;
    document.getElementById("secsBox").innerHTML = seconds;

    var timer = setTimeout('cdtd()',1000);

}

function countdown () {
    var end = new Date("May 31, 2013 00:01:00");
    var start = new Date();
    var timeDiff = end.getTime() - start.getTime();
    if (timeDiff <= 0) {
        clearTimeout (timer);
        document.write("Deal Ended");
    }
    var seconds = Math.floor(timeDiff / 1000);
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);
    var days = Math.floor(hours / 24);
    hours %= 24;
    minutes %= 60;
    seconds %=60;

    document.getElementById("daysBox").innerHTML = days;
    document.getElementById("hoursBox").innerHTML = hours;
    document.getElementById("minsBox").innerHTML = minutes;
    document.getElementById("secsBox").innerHTML = seconds;

    var timer = setTimeout('countdown()',1000);

}

function cdtd3 () {
    var end = new Date("December 25, 2013 00:01:00");
    var start = new Date();
    var timeDiff = end.getTime() - start.getTime();
    if (timeDiff <= 0) {
        clearTimeout (timer);
        document.write("Deal Ended");
    }
    var seconds = Math.floor(timeDiff / 1000);
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);
    var days = Math.floor(hours / 24);
    hours %= 24;
    minutes %= 60;
    seconds %=60;

    document.getElementById("daysBox").innerHTML = days;
    document.getElementById("hoursBox").innerHTML = hours;
    document.getElementById("minsBox").innerHTML = minutes;
    document.getElementById("secsBox").innerHTML = seconds;

    var timer = setTimeout('cdtd3()',1000);

}

function cdtd4 () {
    var end = new Date("December 25, 2013 00:01:00");
    var start = new Date();
    var timeDiff = end.getTime() - start.getTime();
    if (timeDiff <= 0) {
        clearTimeout (timer);
        document.write("Deal Ended");
    }
    var seconds = Math.floor(timeDiff / 1000);
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);
    var days = Math.floor(hours / 24);
    hours %= 24;
    minutes %= 60;
    seconds %=60;

    document.getElementById("daysBox").innerHTML = days;
    document.getElementById("hoursBox").innerHTML = hours;
    document.getElementById("minsBox").innerHTML = minutes;
    document.getElementById("secsBox").innerHTML = seconds;

    var timer = setTimeout('cdtd4()',1000);

}

function cdtd5 () {
    var end = new Date("December 25, 2013 00:01:00");
    var start = new Date();
    var timeDiff = end.getTime() - start.getTime();
    if (timeDiff <= 0) {
        clearTimeout (timer);
        document.write("Deal Ended");
    }
    var seconds = Math.floor(timeDiff / 1000);
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);
    var days = Math.floor(hours / 24);
    hours %= 24;
    minutes %= 60;
    seconds %=60;

    document.getElementById("daysBox").innerHTML = days;
    document.getElementById("hoursBox").innerHTML = hours;
    document.getElementById("minsBox").innerHTML = minutes;
    document.getElementById("secsBox").innerHTML = seconds;

    var timer = setTimeout('cdtd5()',1000);

}

function cdtd6 () {
    var end = new Date("December 25, 2013 00:01:00");
    var start = new Date();
    var timeDiff = end.getTime() - start.getTime();
    if (timeDiff <= 0) {
        clearTimeout (timer);
        document.write("Deal Ended");
    }
    var seconds = Math.floor(timeDiff / 1000);
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);
    var days = Math.floor(hours / 24);
    hours %= 24;
    minutes %= 60;
    seconds %=60;

    document.getElementById("daysBox").innerHTML = days;
    document.getElementById("hoursBox").innerHTML = hours;
    document.getElementById("minsBox").innerHTML = minutes;
    document.getElementById("secsBox").innerHTML = seconds;

    var timer = setTimeout('cdtd6()',1000);

}

3 个答案:

答案 0 :(得分:1)

您需要解决几个问题:

  1. 每个倒计时器在存储时间字符串时使用相同的元素ID。这就是为什么只有其中一个出现的原因。

  2. 如果您的任何倒计时达到零,document.write()来电将删除整个页面。

  3. 代码一遍又一遍地重复。这应该是所有倒计时的一个常见功能。 (如果你需要再添加一个呢?还有十个?)

  4. 虽然多个计时器可以工作,但您不需要它们。运行一个计时器并从中更新所有显示的时间。

  5. 当您致电setInterval()时,最好将函数引用作为第一个参数而不是字符串传递。

  6. 对这些想法进行一些思考,看看你能想出什么,然后用你的新代码报告。 : - )

答案 1 :(得分:0)

现在你每次调用函数时都会引用一个框来覆盖每个值。

您想使用查询选择器而不是getElementById。

这是一个使用jQuery的简单示例:

var cdtd = function(id,end) {
    var start = new Date();
    var timeDiff = end.getTime() - start.getTime();
    if (timeDiff <= 0) {
            clearTimeout (timer);
            document.write("Deal Ended");
    }

    var seconds = Math.floor(timeDiff / 1000);
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);
    var days = Math.floor(hours / 24);
    hours %= 24;
    minutes %= 60;
    seconds %= 60;
    $( id + " .days").html(days);
    $( id + " .hours").html(hours);
    $( id + " .minutes").html( minutes);
    $( id + " .seconds").html( seconds );
    console.log(id + " .hoursBox",$( id + " .hoursBox").length,id,end,hours,minutes,seconds)
    var timer = setTimeout(function(){cdtd(id,end)},1000);
}
cdtd("#counter1",new Date("December 25, 2014 00:01:00"));
cdtd("#counter2",new Date("October 31, 2014 00:01:00"));
cdtd("#counter3",new Date("January 1, 2014 00:01:00"));

http://plnkr.co/8LrtWvfGnZWyRYl2RlWd

答案 2 :(得分:0)

@Shanimal我在我的网站上使用了这个代码,问题是,当计时器达到0000然后它进入另一个页面并显示交易结束。我想要什么,当计时器达到0000时,它必须在特定div上的同一页面上显示消息。它不应该去另一个页面。我已经尝试删除     if(timeDiff&lt; = 0){             clearTimeout(计时器);             document.write(“Deal Ended”);     }

它没有显示任何消息,即使它达到0000它再次从-1 - 1 -1 -1开始计数