带有JQuery的HTML5 + Jscript,setInterval问题 [英] HTML5 + Jscript with JQuery, setInterval problem

查看:116
本文介绍了带有JQuery的HTML5 + Jscript,setInterval问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有以下html文档:

 <!DOCTYPE html PUBLIC -  // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/ TR / XHTML1 / DTD / XHTML1-transitional.dtd> 

< html lang =en>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8 />
< title>使用JQuery的样本量表< / title>
< / head>
< body>
< canvas id =gaugewidth =200height =200>
您的网络浏览器不支持HTML 5,因为您失败了。
< / canvas>
< script type =text / javascriptsrc =jquery.js>< / script>
< script type =text / javascriptsrc =gauge.js>< / script>
< / body>
< / html>

和gauge.js:

<$ p $ ();
$(

$ start

$ b $函数startRendering(){
setInterval(render();,1000);
}

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

//(绘制仪表的定义函数)
});

render()不会被调用,但是如果我将setInterval行更改为'render() '它确实。
另外,如果我改变setInterval()来包含类似alert('LOL')的东西,那么它确实有效,它似乎不适用于我定义的函数。
在调用函数的末尾加上或不加分号都没有区别,也没有在这个前缀之前加上分号。我的功能。



我试图让这个工作,所以我可以开始使用它来动画的规。任何人都可以看到它为什么不起作用吗?



我讨厌网页开发。

解决方案改变

  setInterval(render();,1000); 

  setInterval(render,1000); 

当你传递一个字符串给setInterval()时,里面的代码是在当前范围外执行。无论如何,传递函数更合适。如果关闭圆括号,函数可以像其他变量一样传递。


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.

解决方案

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.

这篇关于带有JQuery的HTML5 + Jscript,setInterval问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆