DevToolBox免费
博客

JavaScript 事件循环可视化

可视化 JavaScript 事件循环,包含调用栈、任务队列和微任务队列。交互式理解异步执行。

console.log("Start");

setTimeout(() => {
  console.log("setTimeout");
}, 0);

Promise.resolve().then(() => {
  console.log("Promise");
});

console.log("End");
空闲
调用栈
(空)
Web APIs / 定时器
(空)
微任务队列
(空)
任务队列(宏任务)
(空)
(空)
调用栈
微任务队列
任务队列(宏任务)
Web APIs / 定时器
控制台输出

理解 JavaScript 事件循环

JavaScript 事件循环是处理异步操作的机制。它持续检查调用栈和队列以确定下一步执行什么代码。setTimeout 回调进入任务队列(宏任务),而 Promise 回调进入微任务队列,微任务优先级更高。

核心概念

  • 调用栈首先执行同步代码(后进先出顺序)
  • 微任务(Promise)始终在宏任务(setTimeout)之前运行
  • 每个宏任务之后都会执行所有待处理的微任务
  • 只有当调用栈为空时,事件循环才检查队列
  • setTimeout 等 Web API 在主线程之外运行

常见问题

什么是 JavaScript 事件循环?
JavaScript 事件循环是一种并发模型,允许单线程的 JavaScript 执行非阻塞 I/O 操作。它通过将操作卸载到浏览器(Web API),然后在调用栈为空时将其回调排队执行。
微任务和宏任务有什么区别?
微任务(Promise.then、queueMicrotask)优先级更高,在每个宏任务之后处理。宏任务(setTimeout、setInterval、I/O)每次处理一个,每个宏任务之间会清空所有微任务。
为什么 Promise.then 在 setTimeout 之前执行?
Promise 回调是微任务,在当前同步代码完成后立即处理,在任何宏任务之前。事件循环总是先清空整个微任务队列,然后才处理下一个宏任务。
调用栈为空时会发生什么?
调用栈为空时,事件循环首先检查微任务队列并处理所有待处理的微任务。微任务队列为空后,它从任务队列中取出下一个宏任务推入调用栈执行。
这个工具免费吗?
是的,这个 JavaScript 事件循环可视化工具完全免费。它在您的浏览器中模拟事件循环行为,帮助您理解异步 JavaScript 执行模式。

相关工具

𝕏 Twitterin LinkedIn

评价此工具

3.8 / 5 · 109 人评价

保持更新

获取每周开发技巧和新工具通知。

无垃圾邮件,随时退订。

Enjoy these free tools?

Buy Me a Coffee

How to Use

  1. Enter or paste your data in the input field
  2. Configure any options if available
  3. Click the action button to process
  4. Copy the result to your clipboard

Use Cases

  • Development and debugging workflows
  • Data format conversion
  • Code generation and formatting
  • Quick calculations and validation

FAQ

Is this Javascript Event Loop Visualizer tool free to use?
Yes, the Javascript Event Loop Visualizer tool is completely free. No registration or payment required.
Is my data secure?
Absolutely. All processing happens client-side in your browser. Your data never leaves your device or is sent to any server.
What formats does the Javascript Event Loop Visualizer tool support?
The tool supports all standard formats for IP address calculation. Check the tool interface for specific format options.

💬 User Feedback

Have suggestions or found a bug? Leave a message and we'll get back to you.
0/2000