1. What are microtasks? 2. What are the macro tasks?
3. Case<script> console.log(1) setTimeout(()=>{ console.log("2") },0) Promise.resolve().then(()=>{ console.log('3') }) console.log(4) </script> We found that the order of printing is Why is it in this order? Print 1-4 first. There is definitely no problem. Why is 3 printed first and then 2? Because 3 is 2 is Microtasks are executed earlier than macrotasks. So 3 is executed first and then 2 3.1 Conclusion
4. Code Examples<body> <div id="app"></div> <script> // This section is DOM rendering let app=document.getElementById("app") let cont='<p>I am a p tag</p>' app.append(cont) // DOM rendering ends console.log(1) setTimeout(()=>{ console.log("2") alert('setTimeout2') },0) Promise.resolve().then(()=>{ console.log('3') alert('3') }) console.log(4) </script> </body> 4.1 Code AnalysisExecution analysis of the above code: You must execute Then according to the micro task first and then the macro task It outputs 3 and then pops up 3 Then it says output 2 and pops up Because there is a So then comes So after outputting Then output 2 and then 4.2 Conclusion and Application ScenariosMicrotask>DOM rendering>Macrotask See the following example Application scenarios of this conclusion We have all done echarts. We know when rendering echarts. The node can only be rendered after the DOM of the page is rendered. So some friends will put the request timing in the monitored() life cycle This ensures that the returned data can be rendered normally on the page. In fact, based on the above conclusion. You can request data in create. When I came back. The DOM is definitely rendered. Because the request is a macro task. The execution time of the macro task is after DOM rendering. This is the end of this article about the details of macrotasks and microtasks in You may also be interested in:
|
<<: A few front-end practice summaries of Alipay's new homepage
This article shares the specific code of jQuery t...
Preface Vue Router is the official routing manage...
In a table, you can define the color of the lower...
Table of contents What is maintainable code? Code...
Table of contents 1. Array.at() 2. Array.copyWith...
Install Install ts command globally npm install -...
HTML form tag tutorial, this section mainly expla...
Table of contents Preface Detect Zookeeper servic...
First, you need to determine which fields or fiel...
1. Download centos7 Download address: https://mir...
Table of contents 1. V8 Source 2. V8 Service Targ...
Initially, multiple columns have different conten...
MySQL is divided into installation version and fr...
Table of contents background Provide / Inject Ext...
1. Basic use of firewalld start up: systemctl sta...