0

Hi I'm trying to add several javascript countdowns to a single html page. I have included the .js file below. Right now my page only displays the first countdown.

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
  • 2
    You may want to get familiar with the concept of Object-oriented programming: developer.mozilla.org/en-US/docs/JavaScript/… Commented May 23, 2013 at 17:01
  • One thing for sure, DRY... Commented May 23, 2013 at 17:01
  • All your functions update elements with the same ID. Commented May 23, 2013 at 17:14

3 Answers 3

1

There are several problems you need to fix here:

  1. Each of your countdown timers uses the same element IDs when it stores the time strings. That's why only one of them shows up.

  2. If any of your countdowns reaches zero, the document.write() call will erase the entire page.

  3. The code is repeated over and over again. This should be one common function for all your countdowns. (What if you need to add one more? Ten more?)

  4. While multiple timers would work, you don't need them. Run a single timer and update all your displayed times from it.

  5. When you call setInterval(), it's better to pass a function reference as the first parameter instead of a string.

Give those ideas some thought and see what you can come up with, then report back with your new code. :-)

Sign up to request clarification or add additional context in comments.

Comments

0

Right now you are referencing one box overwriting each value everytime you call the function.

You want to use a query selector instead of getElementById.

Here is a simple example using 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

Comments

0

@Shanimal i m used this code on my website,problem is that when timer reached 0000 then it goes another page and shows deal ended.what i want ,when timer reach 0000 it should must display message on same page on specific div.it should not go for another page.i have tried it by removing if (timeDiff <= 0) { clearTimeout (timer); document.write("Deal Ended"); }

it does not shows any message even when it reached 0000 it start to count again from -1 - 1 -1 -1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.