简体   繁体   English

何时调用 setTimeout 注册的 function?

[英]When is the function registered with setTimeout called?

it maybe called after 'some' normal code is executed?它可能在执行“一些”正常代码后调用? but i not sure... it would be great if some one kind enough to depict a big picture of how javascript code is executed in a browser.但我不确定......如果有一种足以描述 javascript 代码如何在浏览器中执行的大图,那就太好了。

For most of the time the browser execution thread is idle , not running any code.大多数时候浏览器执行线程是空闲的,没有运行任何代码。 When you register some function to be executed using setTimeout , it will be executed not earlier than after given amount of milliseconds.当您注册一些 function 以使用setTimeout执行时,它将不早于给定的毫秒数之后执行。

Now: if after given amount of time some other code is executing (like event handler or a long loop), browser worker thread is busy and your function will have to wait.现在:如果在给定时间后执行其他代码(如事件处理程序或长循环),浏览器工作线程正忙,您的 function 将不得不等待。 Consider this code:考虑这段代码:

setTimeout(f, 500);
for(var i = 0; i < 10000000; ++i){
    //...
}

setTimeout returns immediately allowing the execution of the loop. setTimeout立即返回,允许执行循环。 If this loop runs for more than 500 milliseconds, it won't be interrupted and your f function will have to wait.如果这个循环运行超过 500 毫秒,它不会被中断,你的f function 将不得不等待。 The same thing will occur if after eg 490 milliseconds you trigger some lengthy event handler.如果在例如 490 毫秒后触发一些冗长的事件处理程序,也会发生同样的事情。

Consider browser JS worker thread as a queue with a single consumer and multiple producers.将浏览器 JS 工作线程视为具有单个消费者和多个生产者的队列。 Some items in the queue can be picked up immediately (like event handlers), some have to wait until their timeout expires.队列中的一些项目可以立即被拾取(如事件处理程序),一些必须等到它们的超时到期。

Javascript is single threaded and runs with a callstack, which is essentially first come first serve processing. Javascript 是单线程的,使用调用堆栈运行,本质上是先到先服务处理。

The setInterval and setTimeout functions put the passed function into the callstack after the supplied duration has elapsed. setInterval 和 setTimeout 函数在提供的持续时间过去后将传递的 function 放入调用堆栈。 The distinction here is that the results are not executed at that time, they're simply put into the callstack, so if something is already running, it won't execute your function supplied in the timeout until you're finished.这里的区别是结果当时没有执行,它们只是放入调用堆栈,所以如果某些东西已经在运行,它不会执行你在超时中提供的 function 直到你完成。

For example:例如:

var i = 0;
setTimeout(function() { i = 1; }, 10);
for (; i < 1;) {}

You'll never get out of that for loop, even though after 10ms, the function to set i to 1 is called, it's only on the callstack, and won't take affect until the for loop is done.即使在 10 毫秒后,将 i 设置为 1 的 function 被调用,你也永远不会摆脱那个 for 循环,它只在调用堆栈上,并且在 for 循环完成之前不会生效。

Maybe Im not understanding the question.. but this is taken directly from the window spec.也许我不理解这个问题..但这直接取自window规范。

setTimeout(function, milliseconds) This method calls the function once after a specified number of milliseconds elapses, until canceled by a call to clearTimeout. setTimeout(function, milliseconds) 此方法在指定的毫秒数过去后调用一次 function,直到通过调用 clearTimeout 取消。 The methods returns a timerID which may be used in a subsequent call to clearTimeout to cancel the interval.这些方法返回一个 timerID,可以在随后的 clearTimeout 调用中使用它来取消间隔。

Reference参考

Basically the function is called right after the amount of milliseconds you specified has passed.基本上 function 在您指定的毫秒数过去后立即调用。

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

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