为什么这个JavaScript计时器会崩溃我的网页?

时间:2013-10-02 17:13:29

标签: javascript timer countdown

所以我试图把一个计时器放到一个特定日期的特定时间。我找到了一些在线工作的代码并将其改编为此

var Timer;
var TotalSeconds;

function CreateTimer(TimerID, Time)
{
    Timer = document.getElementByID(TimerID);
    TotalSeconds = Time;
    UpdateTimer()
    window.setTimeout("Tick()", 1000);
}
window.setTimeout("Tick()", 1000);
function Tick()
{
    if (TotalSeconds <= 0)
    {
        alert("Time's up!")
        return;
    }

    TotalSeconds -= 1;
    UpdateTimer()
    window.setTimeout("Tick()", 1000);
}

function UpdateTimer()
{
    var Seconds = TotalSeconds;
    var Days = Math.floor(Seconds / 86400);
    Seconds -= Days * 86400;

    var Hours = Math.floor(Seconds / 3600);
    Seconds -= Hours * (3600);

    var Minutes = Math.floor(Seconds / 60);
    Seconds -= Minutes * (60);


    var TimeStr = ((Days > 0) ? Days + " days " : "") + LeadingZero(Hours) + ":" + LeadingZero(Minutes) + ":" + LeadingZero(Seconds)


    Timer.innerHTML = TimeStr + " until my birthday!";
}


function LeadingZero(Time)
{
    return (Time < 10) ? "0" + Time : + Time;
}

当我在我的网页上启动时,它会崩溃页面。我知道问题不在于我如何将其链接到HTML,因为我在http://writecodeonline.com/javascript/上测试了这段代码,但它也没有在那里工作。有什么建议吗?

0 个答案:

没有答案