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
setTimeout does not work
提问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:
三人解释说:
- (As SLaks mentions) This calls
visualize
immediately, and then passes its return value intosetTimeout
(and sincevisualize
calls itself, it keeps calling itself recursively and you end up with a stack overflowerror). - This passes a function reference into
setTimeout
that, when called, will callvisualize
and pass it thefile
argument. The function we're passing intosetTimeout
will have access to thefile
argument, even though your code has run and returned, because that function is a closureover the context in which it was created, which includesfile
. More: Closures are not complicated - This passes the
visualize
function reference intosetTimeout
(note we don't have()
or(file)
after it) and also passesfile
intosetTimeout
(after the delay). On modern browsers,setTimeout
will pass that on to the function when calling it later.
- (正如 SLaks 提到的)这会
visualize
立即调用,然后将其返回值传递给setTimeout
(并且由于visualize
调用自身,它不断递归调用自身,最终会出现堆栈溢出错误)。 - 这将传递一个函数引用
setTimeout
,当被调用时,将调用visualize
并将file
参数传递给它。我们传入的函数setTimeout
将可以访问file
参数,即使您的代码已经运行并返回,因为该函数是创建它的上下文的闭包,其中包括file
. 更多:闭包并不复杂 - 这将
visualize
函数引用传递到setTimeout
(注意我们没有()
或(file)
在它之后)并且也传递file
到setTimeout
(在延迟之后)。在现代浏览器上,setTimeout
稍后调用时会将其传递给函数。
(There's an important difference between #2 and #3: With #2, if file
is changed between when setTimeout
is called and the timer expires, visualize
will see file
's new value. With #3, though, it won't. Both have their uses.)
(#2 和 #3 之间有一个重要的区别:使用 #2,如果file
在setTimeout
调用时间和计时器到期之间更改,visualize
将看到file
的新值。但是,使用 #3,它不会。两者都有其用途.)
回答by SLaks
setTimeout(visualize(file), 2000)
calls visualize
immediatelyand 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.
也会起作用。