javascript 使用 setTimeout 在自身内部调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7369681/
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
Calling a function inside itself using setTimeout
提问by Vivek
$(document).ready (
function ready() {
var tester = $.ajax({
async: false,
url: "test_parse.php"
}).responseText;
document.getElementById('test').innerHTML = tester;
setTimeout(ready(), 3000);
}
);
Hey guys I want to call a function within itself like this but every time I do this my browser just keeps loading and eventually Apache shuts down (obviously not my expected result). Think you guys could help me figure out a solution? Thank you!
嘿伙计们,我想像这样在自身内部调用一个函数,但是每次我这样做时,我的浏览器都会不断加载,最终 Apache 关闭(显然不是我的预期结果)。觉得你们能帮我找出解决办法吗?谢谢!
回答by Joe
setTimeout takes a function reference:
setTimeout 需要一个函数参考:
setTimeout(ready, 3000);
not
不是
setTimeout(ready(), 3000);
And that being said, I would also do this:
话虽如此,我也会这样做:
$(document).ready (
function ready() {
var tester = $.ajax({
url: "test_parse.php",
success: function (data) {
document.getElementById('test').innerHTML = data;
setTimeout(ready, 3000);
}
})
}
);
Because async: false
will lock up the browser until that data returns from the server
因为async: false
会锁定浏览器,直到数据从服务器返回
回答by Travis Webb
This is wrong:
这是错误的:
setTimeout(ready(), 3000);
This is right:
这是正确的:
setTimeout(ready, 3000);
ready()
is actually invoking the function. ready
is simply a reference to the function, which is what you want.
ready()
实际上是调用函数。ready
只是对函数的引用,这正是您想要的。
回答by Ian Hill
setTimeout expects a function reference as the first parameter, you have a function invocation which is passing the result of calling ready().
setTimeout 需要一个函数引用作为第一个参数,您有一个函数调用,它传递调用 ready() 的结果。
This is causing an infinite loop.
这导致了无限循环。
You need to pass in "ready", not "ready()"
您需要传入“ready”,而不是“ready()”
setTimeout(ready, 3000);
And if you're trying to queue ajax requests that happen in a structured order, you'll want to fire the setTimeout on success after the previous ajax call, not immediately, otherwise you'll have ajax results returning and updating at arbitrary intervals depending on how the server responds to each request.
而且,如果您尝试对以结构化顺序发生的 ajax 请求进行排队,您将希望在上一次 ajax 调用后成功触发 setTimeout,而不是立即触发,否则您将有 ajax 结果返回并以任意时间间隔更新,具体取决于关于服务器如何响应每个请求。
$.ajax({
// your ajax settings
}).success(function () {
// fire off the next one 3 secs after the last one completes
setTimeout(ready, 3000);
});
This is better than using the async: false setting, which blocks the browser.
这比使用阻止浏览器的 async: false 设置更好。
回答by lowe_22
Why are you trying to call the function within itself? Surely all you need to do is move setTimeout outside of the function, then call ready() every 3000ms? What'l do you want your output to be?
为什么要尝试在其内部调用该函数?当然,您需要做的就是将 setTimeout 移到函数之外,然后每 3000 毫秒调用一次 ready()?你希望你的输出是什么?