1. Four concepts 1. JavaScript is single-threaded Single-threaded means that our 2. Task QueueIn order to solve the above-mentioned queuing problem, a task queue is created. When the browser has an asynchronous task with a result, it will be added to the task queue for future execution, and other tasks will be executed synchronously on the main thread. It should be noted here that the timing of adding tasks to the task queue is after the asynchronous task has a result. In fact, what exists in the task queue is the callback function of the asynchronous task. 3. Synchronous tasks and asynchronous tasksSynchronous tasks in Js programs refer to tasks executed in the main thread, and asynchronous tasks refer to tasks that enter the task queue. 4. Javascript execution stackAll synchronous tasks are executed on the main thread, forming an execution stack. When the task on the main thread is completed, the task is taken out from the task queue for execution. var name = "zhouwei"; setTimeout(() => { console.log(1); }, 1000); console.log(name); The above code is executed in the browser as follows. We understand the code of the program's global execution environment as the code wrapped in a main function. The execution stack of this code changes as shown in the following figure:
Event Loop The process of Js execution stack continuously reading tasks from the task queue and executing them is called We know that the task queue stores the results of asynchronous tasks, so what are the asynchronous tasks?
There are many events in
The asynchronous task callbacks entering the task queue are divided into macrotasks and microtasks. The rules for the Js execution stack to execute macrotasks and microtasks are shown in the figure below. The Js execution stack first executes a macro task (global code) -> reads all micro tasks from the task queue -> UI rendering (browser rendering interface) -> reads a macro task from the task queue -> all micro tasks -> UI rendering -> …
What are macrotasks?
What are microtasks?
setTimeout delay issue Generally speaking, the execution time of the callback in 3. Actual combatPractice the following code output: console.log(1); setTimeout(() => { console.log(2); Promise.resolve().then(() => { console.log(3) }); }); new Promise(resolve => { console.log(4); setTimeout(() => { console.log(5); }); resolve(6) }).then(data => { console.log(data); }) setTimeout(() => { console.log(7); }) console.log(8); Use the js execution mechanism we mentioned above to analyze this question: 1: Execute the synchronous code output in the global task:
It should be noted here that the 2: At this time, there are three // The macro task at this time is setTimeout(() => { console.log(2); Promise.resolve().then(() => { console.log(3) }); }); setTimeout(() => { console.log(5); }); setTimeout(() => { console.log(7); }) // At this time, the microtask is then(data => { console.log(data); })
3: Then execute the first macro task setTimeout(() => { console.log(2); Promise.resolve().then(() => { console.log(3) }); });
In this macrotask, a microtask is added to the task queue. At this point the task queue has a new microtask. 4: Execute a microtask, output: 3 then(() => { console.log(3) }); 5: Continue to execute the task according to the rules, output: 5, 7 The overall output is:
Is this your answer? Summarize:
This is the end of this article about the Event Loop of You may also be interested in:
|
<<: How to implement paging query in MySQL
>>: The basic principles and detailed usage of viewport
This article is used to record the installation o...
Table of contents 1. Calculated properties 1.1 Ba...
[LeetCode] 196.Delete Duplicate Emails Write a SQ...
This article shares the specific code of React to...
Computed properties Sometimes we put too much log...
I searched a lot online and found that many of th...
Table of contents 1. What is the life cycle 2. Th...
How to install MySQL 5.7 in Ubuntu 16.04? Install...
This article example shares the specific code of ...
Table of contents Overview Property settings Proc...
This article uses examples to illustrate the use ...
history route History mode refers to the mode of ...
As more and more developers use SASS, we need to ...
Preface Before starting this article, let’s do a ...
1. css: dragTable.css @charset "UTF-8";...