0

我正在关注有关在 JavaScript 中创建计时器的教程,但我无法让它显示在我的浏览器上。我正在使用 Dreamweaver,它在“实时视图”中很好地显示了计时器,但在浏览器窗口中没有显示任何内容。我错过了什么?

这是主要的 HTML 页面:

<html>
    <head>
        <script type="text/javascript" src="/Timer2.js" />
    </head>
    <body>
        <div id='timer' />
        <script type="text/javascript">
            window.onload = CreateTimer("timer", 90);
        </script>
    </head>
    </body>
</html>

这是 JavaScript 计时器功能。timer2.js

var Timer;
var TotalSeconds;

function CreateTimer(TimerID, Time) {
    Timer = document.getElementById(TimerID);
    TotalSeconds = Time;

    UpdateTimer()
    window.setTimeout("Tick()", 1000);
}

function Tick() {
    if (TotalSeconds <= 0) {
        alert("Time's up!")
        return;
    }

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

function UpdateTimer() {
    Timer.innerHTML = TotalSeconds;
}

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


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

2 回答 2

4

您不能自行关闭脚本标签。你必须写<script ....></script>

于 2012-08-08T22:59:29.140 回答
1

<div id="timer"></div>和没有<div id='timer' />

删除</head>代码底部的您,如果您的脚本再次无法运行,请尝试src="Timer2.js"不使用“/”

于 2012-08-08T23:05:42.483 回答