2

Hello, I have some problems with this code.

var second = 0;
    // Set the end time of the campaign on, Year, Month, Day, Hour, Minute only.
    countdown('clock', 2011, 8, 31, 23, 59, second);

    function countdown(clockID, year, month, day, hour, minute, second) {
        Today = new Date();
        Todays_Year = Today.getFullYear();
        Todays_Month = Today.getMonth();
        todaysDate = (new Date(Todays_Year, Todays_Month, Today.getDate(),
                         Today.getHours(), Today.getMinutes(), Today.getSeconds())).getTime();
        targetDate = (new Date(year, month - 1, day, hour, minute, 00)).getTime();

        //Find their difference, and convert that into seconds.                  
        timeLeft = Math.round((targetDate - todaysDate) / 1000);

        if (timeLeft < 0)
            timeLeft = 0;

        if (timeLeft == 0) {
            $('#col2 a').addClass('ended').html('SLUTS�LD').click(function () { return false; });
        }

        // Calculates the time that is left
        days = Math.floor(timeLeft / (60 * 60 * 24));

        timeLeft %= (60 * 60 * 24);
        hours = Math.floor(timeLeft / (60 * 60));

        timeLeft %= (60 * 60);
        minutes = Math.floor(timeLeft / 60);

        timeLeft %= 60;
        seconds = timeLeft;

        var clock = document.getElementById('clock');
        clock.innerHTML = '<span class="days time">' + days + '</span>';
        clock.innerHTML += '<span class="hours time">' + hours + '</span>';
        clock.innerHTML += '<span class="minutes time">' + minutes + '</span>';


        //Recursive call, keeps the clock ticking.
        second += 1000;
        setTimeout("countdown(clock, year, month, day, hour, minute, second)", 1000);

At the moment i get "countdown is not defined (om the last line)". And sometimes i get "too much recursion". Have I made any obvious mistakes here?

Thanks

4
  • 1
    Just from a better coding practice perspective, instead of calling setTimeout with a string (something you may wish to avoid), you can call an anonimous function which will then call the countdown function. setTimeout(function(){ countdown(clock, year, month, day, hour, minute, second); }, 1000); Commented Jul 29, 2011 at 8:16
  • Thanks, but the examples on w3schools wrapped the function names in quotes. Thats why i did it. Commented Jul 29, 2011 at 8:23
  • 2
    Well, many people think that w3cschools is not a good reference. For example, take a look at w3fools.com. Commented Jul 29, 2011 at 8:31
  • I suggest you to read this bonsaiden.github.com/JavaScript-Garden thoroughly and bookmark it. It's really good javascript practices and code. Commented Jul 29, 2011 at 8:42

2 Answers 2

2

This is working ok for me

http://jsfiddle.net/cL8ez/7/

Made the corrections I suggested in the various comments.

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

Comments

0

If you dont mind using a library try copy pasting this code :P:

<script language="JavaScript">
TargetDate = "12/31/2020 5:00 AM";
BackColor = "palegreen";
ForeColor = "navy";
CountActive = true;
CountStepper = -1;
LeadingZero = true;
DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
FinishMessage = "It is finally here!";
</script>
<script language="JavaScript" src="http://scripts.hashemian.com/js/countdown.js"></script>

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.