开发者

javascript: simple countdown timer problem

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(funct开发者_JAVA技巧ion () { 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


This is working ok for me

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

Made the corrections I suggested in the various comments.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜