为什么使用setTimeout函数会立即执行?

发布于 2024-11-30 21:51:13 字数 165 浏览 3 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

岁吢 2024-12-07 21:51:13

您将立即调用该函数并安排其返回值。

使用:

setTimeout(testFunction, 2000);
                       ^

注意:没有括号。

You're calling the function immediately and scheduling its return value.

Use:

setTimeout(testFunction, 2000);
                       ^

Notice: no parens.

以往的大感动 2024-12-07 21:51:13

删除括号

setTimeout(testfunction(), 2000);

如果您想向函数发送参数,您可以创建一个匿名函数,然后该函数将调用您想要的函数。

setTimeout(function() {

    testfunction('hello');

}, 2000);

编辑

有人建议发送一个字符串作为 setTimeout 的第一个参数。我建议不要遵循这一点,并且永远不要发送字符串作为 setTimeout 第一个参数,因为将使用 eval 函数。这是不好的做法,应该尽可能避免。

Remove the parenthesis

setTimeout(testfunction(), 2000);

If you want to send parameters to the function, you can create an anonymous function which will then call your desired function.

setTimeout(function() {

    testfunction('hello');

}, 2000);

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.

遥远的她 2024-12-07 21:51:13

删除 testfunction 名称后面的括号:

setTimeout(testfunction, 2000);

原因是 setTimeout 的第一个参数应该是函数引用,而不是函数的返回值功能。在您的代码中,立即调用 testfunction 并将返回值发送到 setTimeout

Remove the parentheses after the testfunction name:

setTimeout(testfunction, 2000);

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 to setTimeout.

安静被遗忘 2024-12-07 21:51:13

我在这里看到了很多答案,但我只是想花一些时间来解释一下问题的根本原因。

实际上 setTimeOut() 函数是一个异步函数,当您将函数作为参数之一传递给 setTimeOut() 函数时,您的脚本实际上不想浪费您的时间,而是希望尽快执行您传递的函数。

因此,有几种方法可以绕过这个问题。您可以使用回调或承诺。

请参阅此链接以获取快速详细信息:https://www.w3schools.com/js/js_promise.asp

我将向您展示如何使用回调来实现您想要实现的目标。

通过这样做 -

setTimeOut(your_function_name_without_parentheses, <number of milliseconds>)

你实际上可以告诉你的超时,请先执行你自己,然后执行作为第一个参数传递的我的回调函数。

或者,如果您想将参数传递给回调函数,您可以执行类似的操作 -

setTimeOut(() => {your_function_name_with_parentheses(argument_that_you_want_to_pass), <number of milliseconds>})

还有其他方法,上面已经回答了。

另外,我是一个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 -

setTimeOut(your_function_name_without_parentheses, <number of milliseconds>)

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 -

setTimeOut(() => {your_function_name_with_parentheses(argument_that_you_want_to_pass), <number of milliseconds>})

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.

弱骨蛰伏 2024-12-07 21:51:13

首先去掉括号:

setTimeout(testfunction, 2000);

然后,如果你想在setTimeout函数中传递参数,可以这样传递:

 setTimeout(testfunction, 2000, param1, param2);

注意:你可以根据你的函数传递多个参数要求。

First remove the parenthesis:

setTimeout(testfunction, 2000);

And then, if you want to pass parameters in setTimeout function, you can pass in this way:

 setTimeout(testfunction, 2000, param1, param2);

Note: You can pass multiple parameters according to your function requirement.

魔法少女 2024-12-07 21:51:13

好吧,您可能已经得到答案,但我正在解释原因和解决方案。您可以通过两种方式在所需的时间后调用函数。

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

最偏执的依靠 2024-12-07 21:51:13

删除括号,当前您正在立即调用该函数。当前传递给 setTimeout 的是 testfunction() 调用的返回值,但您应该传递给 setTimeout 作为第一个参数的是函数引用

setTimeout(testfunction, 2000);

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

setTimeout(testfunction, 2000);
两仪 2024-12-07 21:51:13

如果任何人遇到与立即调用函数和使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文