HTML5 + Jscript与JQuery,setInterval问题

发布于 2024-09-19 20:00:38 字数 1322 浏览 5 评论 0原文

在我把桌子上的所有东西都扔出去之前,请有人告诉我为什么这不起作用。

我有以下 html 文档:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html"; charset=utf-8 />
    <title>Sample Gauge using JQuery</title>
</head>
<body>
    <canvas id="gauge" width="200" height="200">
    Your web browser does not support HTML 5 because you fail.
    </canvas>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" src="gauge.js"></script>
</body>
</html>

并且 gauge.js:

$(document).ready(function () {
    //(canvas gets assigned)

    startRendering();

    function startRendering() {
        setInterval("render();", 1000);
    }

    function render() {
        renderBackground();
        renderNeedle(-172);
    }

    //(Defined functions for rendering a gauge)
});

render() 不会被调用,但如果我将 setInterval 行更改为“render()”,它就会被调用。 另外,如果我更改 setInterval() 以包含类似“alert('LOL')”的内容,那么它确实可以工作,但它似乎不适用于我定义的函数。 要调用的函数末尾有或没有分号没有区别,前缀 this 也没有区别。我的职能。

我正在尝试让它工作,这样我就可以开始使用它来制作仪表动画。谁能看出为什么它不起作用?

我讨厌网络开发。

Please can someone tell me why this isn't working before I defenestrate everything on my desk.

I have the following html document:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html"; charset=utf-8 />
    <title>Sample Gauge using JQuery</title>
</head>
<body>
    <canvas id="gauge" width="200" height="200">
    Your web browser does not support HTML 5 because you fail.
    </canvas>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" src="gauge.js"></script>
</body>
</html>

And gauge.js:

$(document).ready(function () {
    //(canvas gets assigned)

    startRendering();

    function startRendering() {
        setInterval("render();", 1000);
    }

    function render() {
        renderBackground();
        renderNeedle(-172);
    }

    //(Defined functions for rendering a gauge)
});

render() does not get called, but if I change the setInterval line to just 'render()' it does.
Also, if I change setInterval() to contain something like "alert('LOL')" then it does work, it just doesn't seem to work with functions I have defined.
With or without a semicolon at the end of the function(s) to call makes no difference, nor does prefixing this. to my functions.

I'm trying to get this working so I can start using it to animate the gauge. Can anyone see why it isn't working?

I hate web development.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨落成白 2024-09-26 20:00:38

更改

    setInterval("render();", 1000);

    setInterval(render, 1000);

当您将字符串传递给 setInterval() 时,其中的代码将在当前范围之外执行。无论如何,直接传递该函数更为合适。如果去掉括号,函数可以像任何其他变量一样传递。

Change

    setInterval("render();", 1000);

To

    setInterval(render, 1000);

When you pass a string to setInterval(), the code inside is executed outside of the current scope. It's much more appropriate to just pass the function anyway. Functions can be passed around just like any other variable if you leave the parenthesis off.

枕头说它不想醒 2024-09-26 20:00:38

我不确定为什么仅使用“render()”不起作用,但使用此代码可以解决问题。

function startRendering() {
    setInterval(function(){
        render()
    }, 1000);
}

I'm not sure why using just "render()" doesn't work but using this code will fix the problem.

function startRendering() {
    setInterval(function(){
        render()
    }, 1000);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文