Javascript不在浏览器中显示

时间:2012-08-08 22:57:50

标签: javascript html

我正在关注在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;
}

2 个答案:

答案 0 :(得分:4)

您无法自行关闭脚本标记。你必须写<script ....></script>

答案 1 :(得分:1)

撰写<div id="timer"></div>而不是<div id='timer' />

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