Javascript 为什么我使用setTimeout时方法会立即执行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7137401/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:00:27  来源:igfitidea点击:

Why is the method executed immediately when I use setTimeout?

javascriptsettimeout

提问by Adler

I'm trying to write a simple code with a setTimeout, but the setTimeoutjust won't wait the time it's supposes to and the code execute immediately. What am I doing wrong?

我正在尝试用 a 编写一个简单的代码setTimeout,但是setTimeout它不会等待它应该等待的时间并且代码会立即执行。我究竟做错了什么?

setTimeout(testfunction(), 2000);

回答by Mat

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

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

Use:

用:

setTimeout(testFunction, 2000);
                       ^

Notice: no parens.

注意:没有括号。

回答by Jose Faeti

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.

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

回答by Emil Vikstr?m

Remove the parenthesis after the testfunction name:

去掉 testfunction 名称后的括号:

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, testfunctionis called immediately and the return value is sent to setTimeout.

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

回答by shaILU

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.

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

1. setTimeout("FUNC_NAME ()', TIME_IN_MS);
这里双引号内的 FUNC_NAME 是你想在 TIME_IN_MS 毫秒后调用的原始函数。这是因为如果你不加引号,那么当 java 脚本被解释时,函数会立即执行,你的目的会被打败。为了让解释器跳过我们需要在这里加引号的语句
。2. setTimeout(function () {FUNC_NAME ()}, TIME_IN_MS);
这里创建了匿名函数,告诉解释器执行 if在一定时间之后而不是评估时间。

Thanks shaILU

谢谢 shaILU