简体   繁体   English

当我将 setTimeout 的结果分配给一个变量时,即使我不调用我的变量,setTimeout 中的 function 如何运行?

[英]When I assign to a variable the result of setTimeout, how does the function inside setTimeout run even though I don't call my variable?

Please go through below code,请通过以下代码拨打 go,

<button onclick="myVar = setTimeout(myFunction, 3000)">Try it</button>

When the button is clicked, it display my function;单击按钮时,显示我的 function; but myVar variable is assigned to a function and myVar is not called again, how does it execute it?但是 myVar 变量赋值给了一个 function 而 myVar 没有被再次调用,它是如何执行的呢?

Complete code in below link完整代码在下面链接

https://www.w3schools.com/js/tryit.asp?filename=tryjs_settimeout2 https://www.w3schools.com/js/tryit.asp?filename=tryjs_settimeout2

Clicking the button runs the setTimeout function, which starts a timer inside Javascript单击该按钮运行 setTimeout function,它会在 Javascript 中启动一个计时器

When you run setTimeout , with the name of a function and a number of milliseconds, it starts an internal timer in Javascript.当您运行名称为 function 和毫秒数的setTimeout时,它会在 Javascript 中启动一个内部计时器。

You do not have to do anything to later trigger that function: Javascript will automatically do so, once the time is up (or as soon as possible afterwards, if Javascript is busy).稍后您无需执行任何操作即可触发 function:Javascript 会在时间结束后自动执行此操作(如果 Javascript 正忙,则稍后尽快)。

Here is your original version (shortened):这是您的原始版本(缩短):

 function myFunction() { alert("Hello"); }
 <h2>JavaScript Timing</h2> <p>After you click "Try it", you will get an alert after 3 seconds, UNLESS you click "Cancel" during that time.</p> <button onclick="myVar = setTimeout(myFunction, 3000)">Try it</button> <button onclick="clearTimeout(myVar)">Cancel</button>

Showing that you don't need the "myVar="显示您不需要“myVar=”

You only needed the myVar= because you wanted to be able to cancel the timer.您只需要myVar=因为您希望能够取消计时器。 When you call setTimeout, it (immediately) returns a handle to the timer.当您调用 setTimeout 时,它(立即)返回计时器的句柄。 You can use that handle to cancel the timer.您可以使用该句柄来取消计时器。

If you don't need the ability to cancel, you don't need the myVar= .如果您不需要取消功能,则不需要myVar= Just call setTimeout, like this:只需调用 setTimeout,如下所示:

 function myFunction() { alert("Hello"); }
 <h2>JavaScript Timing</h2> <p>After you click "Try it", you will get an alert after 3 seconds.</p> <button onclick="setTimeout(myFunction, 3000)">Try it</button>

Showing that you don't even need the separate Javascript function显示您甚至不需要单独的 Javascript function

setTimeout can be given a full definition of a function, rather than just a name. setTimeout可以给出一个 function 的完整定义,而不仅仅是一个名称。

 <button onclick="setTimeout(function(){alert('Hello')}, 3000)">Try it</button>

How setTimeout works setTimeout 如何工作

Javascript maintains a list of things that it has been asked to do at intervals (with setInterval ) or at particular times (with setTimeout ). Javascript 维护着一个列表,其中列出了它被要求每隔一段时间(用setInterval )或在特定时间(用setTimeout )做的事情。

It generally gets on with the program it is running, but eventually runs out of things to do.它通常会继续运行它正在运行的程序,但最终会无事可做。 Then it starts going through those lists of pending tasks, to see if enough time has passed for them to be ready to execute.然后它开始遍历那些待处理任务的列表,看看是否有足够的时间让它们准备好执行。

This process, called the "event loop" is quite complicated.这个称为“事件循环”的过程非常复杂。 Fortunately we don't have to worry about it, other than bear in mind that it is not magic.幸运的是我们不必担心,只要记住它不是魔法。 If we have a program that requires a long run of processing, say 1 minute, then during that time Javascript will not be looking in the queues of interval s and timeout s.如果我们有一个需要长时间运行的程序,比如 1 分钟,那么在此期间 Javascript 将不会查看interval s 和timeout s 的队列。 When it finishes the program task (or is waiting for.network interaction etc), it will start looking at that.当它完成程序任务(或正在等待网络交互等)时,它会开始查看那个。

Calling a function inside of a variable declaration will execute the function and assign its return value to the variable.在变量声明中调用 function 将执行 function 并将其返回值分配给变量。

Assigning a function to a variable, without calling, stores the function object inside of it.将 function 分配给一个变量,而不调用,将 function object 存储在其中。 The variable can then be called to execute the function.然后可以调用该变量来执行 function。

In this case, the first is true:在这种情况下,第一个是正确的:

const myVar = setTimeout(func, 1000)

Because setTimeout is called inside the declaration, it executes.因为在声明中调用了 setTimeout,所以它会执行。 Its return value, which consists in a timer id, gets assigned to the variable.它的返回值包含在一个计时器 ID 中,并被分配给该变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM