为什么使用setTimeout函数会立即执行?
我正在尝试使用 setTimeout
编写简单的代码,但是 setTimeout
只是不会等待它应该等待的时间,并且代码会立即执行。我做错了什么?
setTimeout(testfunction(), 2000);
I'm trying to write simple code with a setTimeout
, but the setTimeout
just won't wait the time it's supposed to and the code executes immediately. What am I doing wrong?
setTimeout(testfunction(), 2000);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您将立即调用该函数并安排其返回值。
使用:
注意:没有括号。
You're calling the function immediately and scheduling its return value.
Use:
Notice: no parens.
删除括号
如果您想向函数发送参数,您可以创建一个匿名函数,然后该函数将调用您想要的函数。
编辑
有人建议发送一个字符串作为 setTimeout 的第一个参数。我建议不要遵循这一点,并且永远不要发送字符串作为 setTimeout 第一个参数,因为将使用 eval 函数。这是不好的做法,应该尽可能避免。
Remove the parenthesis
If you want to send parameters to the function, you can create an anonymous function which will then call your desired function.
Edit
Someone suggested to send a string as the first parameter of setTimeout. I would suggest not to follow this and never send a string as a setTimeout first parameter, cause the eval function will be used. This is bad practice and should be avoided if possible.
删除
testfunction
名称后面的括号:原因是
setTimeout
的第一个参数应该是函数引用,而不是函数的返回值功能。在您的代码中,立即调用testfunction
并将返回值发送到setTimeout
。Remove the parentheses after the
testfunction
name:The reason is that the first argument to
setTimeout
should be a function reference, not the return value of the function. In your code,testfunction
is called immediately and the return value is sent tosetTimeout
.我在这里看到了很多答案,但我只是想花一些时间来解释一下问题的根本原因。
实际上 setTimeOut() 函数是一个异步函数,当您将函数作为参数之一传递给 setTimeOut() 函数时,您的脚本实际上不想浪费您的时间,而是希望尽快执行您传递的函数。
因此,有几种方法可以绕过这个问题。您可以使用回调或承诺。
请参阅此链接以获取快速详细信息:https://www.w3schools.com/js/js_promise.asp
我将向您展示如何使用回调来实现您想要实现的目标。
通过这样做 -
你实际上可以告诉你的超时,请先执行你自己,然后执行作为第一个参数传递的我的回调函数。
或者,如果您想将参数传递给回调函数,您可以执行类似的操作 -
还有其他方法,上面已经回答了。
另外,我是一个JS新手,如果我的理解有任何差距,请告诉我。
I see a lot of answers here, but I just want to take some time to explain the root cause of the problem.
Actually setTimeOut() function is an asynchronous function and when you pass a function as one of the parameter to the setTimeOut() function, your script actually does not want to waste your time and wants to execute your passed function as soon as possible.
So there are few ways in which you can bypass this. You can either use callbacks or promises.
Refer to this link for quick detail: https://www.w3schools.com/js/js_promise.asp
I will show you how can you use callback to achieve what you want to achieve.
By doing -
You can actually tell your timeout that please execute yourself first and then execute my callback function passed as the first parameter.
Or if you want to pass a parameter to the callback function, you can do something like this -
There are other ways as well which are already answered above.
Also, I am a newbie to JS, please let me know if there are any gaps in my understanding.
首先去掉括号:
然后,如果你想在
setTimeout
函数中传递参数,可以这样传递:注意:你可以根据你的函数传递多个参数要求。
First remove the parenthesis:
And then, if you want to pass parameters in
setTimeout
function, you can pass in this way:Note: You can pass multiple parameters according to your function requirement.
好吧,您可能已经得到答案,但我正在解释原因和解决方案。您可以通过两种方式在所需的时间后调用函数。
1. setTimeout("FUNC_NAME ()', TIME_IN_MS);
这里双引号内的 FUNC_NAME 是您要在 TIME_IN_MS 毫秒后调用的原始函数。这是因为如果你不加引号,那么在解释 java 脚本时,函数将立即执行,你的目的就会落空。为了让解释器跳过该语句,我们需要在此处加引号。
2. setTimeout(function () {FUNC_NAME ()}, TIME_IN_MS);
这里创建了匿名函数,告诉解释器在一定时间后执行 if 而不是评估时间。
谢谢
沙鲁
Well you might have got the answer but I am explaining the cause and solution. There are two ways in which you can call a function after required amount of time.
1. setTimeout("FUNC_NAME ()', TIME_IN_MS);
Here FUNC_NAME inside double quotes is the original function you want to call after TIME_IN_MS milliseconds. This is because if you do not put the quotes then while java script is getting interpreted the function would be immediately executed and your purpose would get defeated. To let interpreter skip the statement we need to put quotes here.
2. setTimeout(function () {FUNC_NAME ()}, TIME_IN_MS);
Here anonymous function is created that tells interpreter to execute if after certain time instead of evaluating time.
Thanks
shaILU
删除括号,当前您正在立即调用该函数。当前传递给 setTimeout 的是 testfunction() 调用的返回值,但您应该传递给 setTimeout 作为第一个参数的是函数引用
Remove the parenthesis, currently you are invoking the function immediately. What is currently being passed to setTimeout is the returned value from the testfunction() call, but what you should pass to setTimeout as the first argument is a function reference
如果任何人遇到与立即调用函数和使用 Node.js 无关的问题,则您的超时值可能太大,导致延迟为默认值 1。
例如
setTimeout(testFunction, 21474836471) == setTimeout (testFunction, 1)
当延迟大于2147483647或小于1时,延迟将设置为1。非整数延迟将被截断为整数。
Anyone experiencing issues not related to calling the function immediately and using Node.js, your timeout value may be too large causing the delay to default 1.
e.g.
setTimeout(testFunction, 21474836471) == setTimeout(testFunction, 1)
When delay is larger than 2147483647 or less than 1, the delay will be set to 1. Non-integer delays are truncated to an integer.