更新时间:2022-04-06 09:28:49
在计时器到达指定时间时,事件队列没有添加任何内容。
Nothing is added to the event queue UNTIL the timer reaches its appointed time.
所以,事件的顺序如下:
So, the sequence of events is as follows:
func1
计时器为500毫秒。 func2
计时器250毫秒。。
func2
计时器fires(JS引擎内部)及其回调被添加到事件队列中。 func1
计时器触发(内部到JS引擎)并将其回调添加到事件队列中。而
旋转循环终于完成了当前的线程JS完成。 func2
回调并执行它。 func1
回调并执行它。func1
timer for 500ms.func2
timer for 250ms.while
spin loop.func2
timer fires (internal to the JS engine) and its callback is added to the event queue.func1
timer fires (internal to the JS engine) and its callback is added to the event queue.while
spin loop finally finishes and the current thread of JS finishes.func2
callback and executes it.func1
callback and executes it.因此,你得到输出你看到 func2
回调执行assoon为而
旋转循环完成,然后 func1
回调执行。
And thus, you get the output you see with the func2
callback executing assoon as the while
spin loop is done, then the func1
callback executing.