JavaScript 不显示在浏览器中

标签 javascript html

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

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

最佳答案

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

关于JavaScript 不显示在浏览器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11874424/

相关文章:

javascript - 对 div 元素进行动画处理,改变其在 div 列表中的位置

javascript - 如何使用照片引用在 angularjs 中获取谷歌地点照片

javascript - 如何停止 Django 线程

javascript - 如何动态重命名每个 jQuery clone div 子元素 id

html - 输入文本字段的 CSS 代码

html - Angular html 无法正确加载

html - Angular 结合 2 个输出事件来调用 1 个函数

php - 将页面从 html 转换为 php 后的位置问题

javascript - 使用谷歌翻译自动翻译网页

javascript - angularjs 中的客户端验证