Javascript 设置超时不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8375962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:44:22  来源:igfitidea点击:

setTimeout does not work

javascriptjqueryowl

提问by JasperTack

I want to load an OWL file before executing other (visualisation-)scripts. To do this I tried everything from

我想在执行其他(可视化)脚本之前加载一个 OWL 文件。为此,我尝试了所有方法

$(document).ready

to

function visualize (file) {
if (!file)
    {setTimeout(visualize(file), 2000)}
else
    {jQuery(function($){visFeaturePool.init(file)})}}

I think it has to be possible with the setTimeout but that isn't working. I throws the error: Uncaught RangeError: Maximum call stack size exceeded, so it doesn't wait, it just recalls the visualize function untill the stack is full.

我认为 setTimeout 必须是可能的,但这不起作用。我抛出错误:Uncaught RangeError: Maximum call stack size exceeded,所以它不会等待,它只是在堆栈已满之前调用可视化函数。

Does anybody know what I am doing wrong? Thanks!

有谁知道我做错了什么?谢谢!

回答by T.J. Crowder

Instead of

代替

// #1
setTimeout(visualize(file), 2000);

you want

你要

// #2
setTimeout(function() {
    visualize(file);
}, 2000);

or on modern browsers, you can provide arguments to pass to the function after the delay:

或者在现代浏览器上,您可以提供参数以在延迟后传递给函数:

// #3
setTimeout(visualize, 2000, file);

Those three explained:

三人解释说:

  1. (As SLaks mentions) This calls visualizeimmediately, and then passes its return value into setTimeout(and since visualizecalls itself, it keeps calling itself recursively and you end up with a stack overflowerror).
  2. This passes a function reference into setTimeoutthat, when called, will call visualizeand pass it the fileargument. The function we're passing into setTimeoutwill have access to the fileargument, even though your code has run and returned, because that function is a closureover the context in which it was created, which includes file. More: Closures are not complicated
  3. This passes the visualizefunction reference into setTimeout(note we don't have ()or (file)after it) and also passes fileinto setTimeout(after the delay). On modern browsers, setTimeoutwill pass that on to the function when calling it later.
  1. (正如 SLaks 提到的)这会visualize立即调用,然后将其返回值传递给setTimeout(并且由于visualize调用自身,它不断递归调用自身,最终会出现堆栈溢出错误)。
  2. 这将传递一个函数引用setTimeout,当被调用时,将调用visualize并将file参数传递给它。我们传入的函数setTimeout将可以访问file参数,即使您的代码已经运行并返回,因为该函数是创建它的上下文的闭包,其中包括file. 更多:闭包并不复杂
  3. 这将visualize函数引用传递到setTimeout(注意我们没有()(file)在它之后)并且也传递filesetTimeout(在延迟之后)。在现代浏览器上,setTimeout稍后调用时会将其传递给函数。

(There's an important difference between #2 and #3: With #2, if fileis changed between when setTimeoutis called and the timer expires, visualizewill see file's new value. With #3, though, it won't. Both have their uses.)

(#2 和 #3 之间有一个重要的区别:使用 #2,如果filesetTimeout调用时间和计时器到期之间更改,visualize将看到file的新值。但是,使用 #3,它不会。两者都有其用途.)

回答by SLaks

setTimeout(visualize(file), 2000)calls visualizeimmediatelyand passes its result to setTimeout, just like any other function call.

setTimeout(visualize(file), 2000)visualize立即调用并将其结果传递给setTimeout,就像任何其他函数调用一样。

回答by Sebastián Grignoli

Try this:

尝试这个:

function visualize (file) {
  if (!file)
    {setTimeout(function(){visualize(file);}, 2000)}
  else
    {jQuery(function($){visFeaturePool.init(file)})}}

This way you are giving setTimeout an anonymous function that will be executed when scheduled, and you can pass parameters to visualize using a closure like file.

通过这种方式,您可以为 setTimeout 提供一个匿名函数,该函数将在计划时执行,并且您可以使用像file.

回答by neonguru

setTimeout(visualize, 2000, file);

will also work.

也会起作用。