Skip to content

Commit

Permalink
docs: update issue 348
Browse files Browse the repository at this point in the history
  • Loading branch information
toFrankie committed Aug 18, 2024
1 parent 6105ca9 commit 291d0bd
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions archives/2024/348.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: 通过两个例子再探 Event Loop
number: '#348'
link: 'https://github.com/toFrankie/blog/issues/348'
created_at: '2024-08-11 16:42:16'
updated_at: '2024-08-18 15:12:32'
updated_at: '2024-08-18 16:09:13'
labels:
- 前端
- JS
Expand Down Expand Up @@ -78,7 +78,7 @@ while (true) {

用户每点击按钮一次,就会产生一个任务,并添加到任务队列。

除此之外,与用户发生交互而产生的所有事件(比如双击、文本选择、页面滚动、键盘输入等)、setTimeout、setInterval、I/O 操作等都属于任务。
除此之外,与用户发生交互而产生的所有事件回调(比如双击、文本选择、页面滚动、键盘输入等)、setTimeout、setInterval\<script> 块、I/O 操作等都属于任务。

> 任务里也可以产生新任务。
Expand Down Expand Up @@ -190,10 +190,26 @@ while (true) {
3. 如果当前 Event Loop 里的微任务队列不为空,将一直循环直至微队列为空:
1. 在微任务队列里取出的第一个微任务(此处的出队,就是先进先出的出队)
2. 执行该 microtask
4. 将检查点标准设为 false
4. 将检查点标志设为 false
4. 重复上述步骤

> 以上为简化后的步骤,为了更简单地介绍 task 和 microtask 执行顺序。
请注意,如果在执行微队列任务的过程中,产生更多新的微任务,它们将会在下一个(宏)任务之前执行。

至此,文章开头的提问之一就有答案。

Event Loop 的不断循环执行是在主线程完成的,而且是单线程的,那么它就会一直重复上述 3.iii 的过程,它将无休止下去,自然无法更新 DOM、执行后面的(宏)任务,因此页面就“卡死”了。

### 有哪些微任务?

在 JavaScript 里会产生微任务的大概有:

- queueMicrotask(Window 或 Web Worker)
- Promise 回调
- MutationObserver 回调
- Object.observe(Deprecated)


## References

Expand Down

0 comments on commit 291d0bd

Please sign in to comment.