Javascript Js:没有函数参数的setTimeOut?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28406978/
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
Js: setTimeOut without function argument?
提问by giorgio79
Why do we need to pass a function to Javascript setTimeOut https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers.setTimeout
为什么我们需要将函数传递给 Javascript setTimeOut https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers.setTimeout
Why cannot we do sg simple like
为什么我们不能像 sg 那样简单
setTimeOut(1000);
Can I pass an empty or nonexistant function in there?
我可以在那里传递一个空的或不存在的函数吗?
I want to simply wait in a for loop after each iteration.
我只想在每次迭代后在 for 循环中等待。
回答by KooiInc
Javascript is single threaded. You can use setTimemoutto postpone an operation, but the thread will continue. So
Javascript 是单线程的。您可以使用setTimemout来推迟一个操作,但线程将继续。所以
function some() {
doStuff();
setTimeout(otherStuff, 1000);
doMoreStuff();
}
will run doStuffand doMoreStuffsubsequently, and will run otherStuffafter a second. That's why it's useless and impossible to use setTimeoutas a delay per se. If doMoreStuffshould be postponed, you should make that the callback for the delay:
将运行doStuff并doMoreStuff随后运行,并将otherStuff在一秒钟后运行。这就是为什么它setTimeout本身没有用,也不可能用作延迟。如果doMoreStuff应该被推迟,你应该为延迟做回调:
function some() {
doStuff();
setTimeout(doMoreStuff, 1000);
}
or both otherstuffand doMoreStuffdelayed:
或两者otherstuff和doMoreStuff延迟:
function some() {
doStuff();
setTimeout(function () {
otherStuff();
doMoreStuff()
}, 1000);
}
Maybe my answer to this SO-questionis usefull too.
也许我对这个问题的回答也很有用。
回答by Suppen
setTimeout registers an even in the event loop. When JavaScript has finished all it's current instructions, it returns to the event loop and waits for an event to happen. When it does, it invokes the callback function, which is the function you pass to setTimeout.
setTimeout 在事件循环中注册一个偶数。当 JavaScript 完成所有当前指令时,它返回到事件循环并等待事件发生。当它这样做时,它调用回调函数,这是您传递给 setTimeout 的函数。
To do a for-loop with setTimeout, do something like this:
要使用 setTimeout 执行 for 循环,请执行以下操作:
function loop(i, n) {
doStuff();
if (i < n) {
setTimeout(function() {
loop(i+1, n);
}, 1000);
}
};
loop(0, 10);
where i is the current iteration and n is max iterations
其中 i 是当前迭代,n 是最大迭代
When JavaScript is not in the event loop, i.e. it's running code, it makes the whole browser unresponsive. Just idling for a second would do exactly this, so if you iterate 10 times over a loop, the browser would be unresponsive for 10 seconds
当 JavaScript 不在事件循环中时,即它正在运行代码,它会使整个浏览器无响应。只需空闲一秒钟就可以做到这一点,因此如果您在循环中迭代 10 次,浏览器将无响应 10 秒
回答by Jitendra Khatri
-->setTimeout(callbackFunction[, interval])
-->setTimeout(callbackFunction[, interval])
This can be understand as you are going to call a function with the specified name(Given by you) after an specified time.
这可以理解为您将在指定时间后调用具有指定名称(由您提供)的函数。
-->You cannot do like this setTimeOut(1000);because 1000 will be treated as callback function As the first parameter of setTimeout()is callback function.
-->你不能这样做,setTimeOut(1000);因为 1000 将被视为回调函数作为回调函数的第一个参数setTimeout()。
You cannot define a function like
你不能定义一个函数
function 1000() {
// Code Resides here.
}
function 1000() {
// Code Resides here.
}
because java script does not allow these type of function names.
因为 java 脚本不允许这些类型的函数名称。
-->You can pass anonymous function to setTimeout()like
-->你可以通过匿名函数来setTimeout()like
setTimeout(function() {
// Code resides here.
}[, interval]);
The above function code will be executed after the given interval. If no interval is given then it will executed immediately.
setTimeout(function() {
// Code resides here.
}[, interval]);
上述功能代码将在给定的时间间隔后执行。如果没有给出间隔,那么它将立即执行。
Note:-Parameters given in square brackets ([, ])are optional to pass.
注意:-方括号([, ] )中给出的参数是可选的。
--> You can refer the below mentioned thread for your use:JavaScript sleep/wait before continuing
--> 您可以参考下面提到的线程供您使用:JavaScript sleep/wait before continue
As this thread contains a function definition which works like sleep()in other languages.
因为这个线程包含一个函数定义,它的工作方式与sleep()其他语言一样。
回答by Mario Figueiredo
This is now possible with Await/Async in a quick one line:
现在可以在快速一行中使用 Await/Async:
await new Promise(resolve => setTimeout(resolve, 1000));
There is another question I think is relevant at Combination of async function + await + setTimeout
我认为还有另一个问题与 async 函数 + await + setTimeout 的组合相关

