Javascript 延迟函数的返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6339151/
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
delay a return of a function
提问by khousuylong
is there anyway to delay a return of a function, using setTimeout()
.
反正有没有延迟函数的返回,使用setTimeout()
.
function foo(){
window.setTimeout(function(){
//do something
}, 500);
//return "some thing but wait till SetTimeout() finished";
}
回答by James Khoury
.setTimeout()
is for running a complete function after a timeout. It is notfor delaying code.
.setTimeout()
用于在超时后运行完整功能。它不是用于延迟代码。
https://developer.mozilla.org/En/Window.setTimeout
https://developer.mozilla.org/En/Window.setTimeout
A good link would be: What is the JavaScript version of sleep()?
一个好的链接是:sleep() 的 JavaScript 版本是什么?
(A good question to ask is why do you need your function to sleep?)
(一个很好的问题是为什么你需要你的函数来睡觉?)
回答by Jj.
You don't want to "delay" the code, because it would lock up the browser thread making your entire browser unusable until the your script's timeout has passed.
您不想“延迟”代码,因为它会锁定浏览器线程,使您的整个浏览器无法使用,直到您的脚本超时为止。
You can either set up an events which listens to a signal fired up after some time has passed. jQuery .bind()
and .trigger()
is what you want http://api.jquery.com/trigger/
您可以设置一个事件,在一段时间后侦听触发的信号。jQuery的.bind()
并且.trigger()
是你想要的http://api.jquery.com/trigger/
Or, you could use a callback function to work with the data you want after the time has ellapsed. So if what you intended to be like this:
或者,您可以使用回调函数在时间过去后处理您想要的数据。所以,如果你打算是这样的:
function myFunc() {
doStuff();
result = delayProcess(5000);
$('#result').html(result);
}
function delayProcess(delay) {
// magic code that delays should go here
return logic;
}
Should be something like this:
应该是这样的:
function myFunc() {
doStuff()
delayProcess(5000, function(result){ // Notice the callback function pass as an argument
$('#result').html(result);
});
}
function delayProcess(delay, callback) {
result = logic;
setTimeout(function(){
callback(result);
});
}
回答by Nelu
Using promises:
使用承诺:
const fetchData = () =>
new Promise(resolve => (
setTimeout(() => resolve(apiCall()), 3000);
));
Answer updated thanks to @NikKyriakides who pointed out async/await is not necessary. I initially had async () => resolve(await apiCall())
.
感谢@NikKyriakides 指出不需要 async/await 的回答更新。我最初有async () => resolve(await apiCall())
.
回答by Endophage
Just call the thing you want to happen after the timeout in the end of the timeout function as below:
只需在超时函数结束时调用您想要在超时后发生的事情,如下所示:
function foo()
{
window.setTimeout(function()
{
//do something
delayedCode(returnValue);
}, 500);
return
}
function delayedCode(value)
{
// do delayed stuff
}
Rather than returning. Put the code that relies on the return value into delayedCode()
and pass in the parameter to the function.
而不是回来。将依赖返回值的代码放入delayedCode()
并传入参数给函数。