简体   繁体   English

为什么此javascript代码根本不起作用?

[英]why doesn't this javascript code work at all?

  function help(n){

    document.write(n + '<br/>');
    if(n==10){ n=1;}
    n++;

    main(n);
 }

 function main(n){

  setTimeout('help(n)',500);

 }

I want it to print 我要打印

1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10

and then again 然后再

1 2 3 4 5 6 etc 1 2 3 4 5 6等

after 0.5 seconds 0.5秒后

I want to do this by using the setTimeout method, but this approach doesn't work at all 我想通过使用setTimeout方法来做到这一点,但是这种方法根本不起作用

My body element is like this 我的身体元素就是这样

<body onload='main(1)'>

Can someone explain me why? 有人可以解释我为什么吗?

Edit : I changed the foo method to help. 编辑 :我更改了foo方法来提供帮助。 I made a mistake while editing my code in order to post this question. 在发布代码时,我在编辑代码时犯了一个错误。

I believe the reason you might get: 我相信您可能会得到的原因:

attempt to run compile-and-go script on a cleared scope 尝试在已清除的范围内运行编译脚本

in Firebug is due to the use of document.write() after the page has loaded. Firebug中的原因是由于页面加载后使用了document.write() This also appears to be a new Firebug JS error. 似乎也是一个新的Firebug JS错误。

Once it runs initially, you are writing to the document after you are allowed to. 最初运行后,您将被允许写入文件。 I tried using document.open() and document.close() , and adding both did not fix the issue. 我尝试使用document.open()document.close() ,添加它们都不能解决问题。

There are better ways, though. 不过,还有更好的方法。 You can use this to replace the body content: 您可以使用它来替换body内容:

document.body.innerHTML = n + '<br/>;

Or this to add to it: 或添加以下内容:

document.body.innerHTML += n + '<br/>;

Or, this (what I would recommend): 或者,这(我会推荐的):

var div = document.createElement('div');
div.innerHTML = n + '<br/>';
document.body.appendChild(div);

Note, do not use setTimeout('help(n)',##) , since that's eval() ing the code to call the function, which is bad practice. 注意,不要使用setTimeout('help(n)',##) ,因为那是eval()编写代码来调用函数的方法,这是一种不好的做法。 Also, avoid the second example above, since in some browsers you may experience performance issues due to the way that .innerHTML appends to a node. 另外,避免使用上面的第二个示例,因为在某些浏览器中,由于.innerHTML附加到节点的方式,您可能会遇到性能问题。 Hence, this is another bad practice to avoid. 因此,这是要避免的另一种不好的做法。

Demonstration of the third method follows. 第三种方法的演示如下。

function help(n){
    var div = document.createElement('div');
    div.innerHTML = n + '<br/>';
    document.body.appendChild(div);

    if (n == 10) {
        n = 1;
    }

    n++;

    main(n);
}

function main(n){
    setTimeout(function(){
        help(n);
    }, 500);
}

main(5);

http://jsfiddle.net/be6He/1 http://jsfiddle.net/be6He/1

function main(n){  
    setTimeout(help,500,n);
}

or for full IE compatibility : 或完全兼容IE:

 function main(n){  
    setTimeout(function(){
        help(n);
    },500);
}

您的setTimeout正在调用一个不存在的函数foo()

setTimeout takes a function reference, not a string. setTimeout采用函数引用,而不是字符串。 Try something like: 尝试类似:

function main(n)
{
  setTimeout(function () {
     help(n);
   }, 500);
}

EDIT: 编辑:

setTimeout can take a string, but this function would not be run within the closure of main and n would be out of scope. setTimeout 可以采用字符串,但是此函数不会在main的闭包内运行,并且n将超出范围。 Plus, it's highly recommended to use a function reference, like the example above. 另外,强烈建议使用函数引用,例如上面的示例。

n is no longer in scope when the help function executes. 当执行帮助功能时, n不再是作用域。 Your main should do something like: 您的main应执行以下操作:

function main(n) {
    window.setTimeout(function() {
        help(n);
    }, 500);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM