javascript:简单倒数计时器问题

时间:2011-07-29 08:04:09

标签: javascript timer settimeout countdown

您好,我的代码存在问题。

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);

目前我得到“倒数未定义(最后一行)”。有时我得到“过多的递归”。我在这里犯过任何明显的错误吗?

由于

2 个答案:

答案 0 :(得分:2)

这对我有用

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

我在各种评论中提出了更正。

答案 1 :(得分:0)

如果您不介意使用库,请尝试复制粘贴此代码: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>