JS//JS operating mechanism (asynchronous single thread)

JS-asynchronous single thread

Insert picture description here
Insert picture description here

JS-JS operating mechanism (asynchronous single thread)

1 knowledge points

1.1 How to understand the single thread of JS

Can only do one thing at a time

1.2 What is an event queue

JS has task priority. Synchronous task>Asynchronous task

1.3 What is asynchronous (synchronous)

Synchronous: send a request, wait for the return, and then send the next request
Asynchronous: send a request, do not wait for the return, you can send the next request at any time

1.4 What is an event loop (event loop)

Synchronous tasks are put into the running stack, while asynchronous tasks are not put into the asynchronous task queue after the set time has passed. After the
synchronous task is executed, the tasks in the asynchronous task queue are put into the running stack, and the running stack is empty after running. Then go to the asynchronous task queue to monitor whether there is a task,
and then execute it again. This loop is the event loop (Event Loop)

1.5 Understand which statements will be placed in the asynchronous task queue or when to execute asynchronous tasks or the front end uses asynchronous scenarios

All "waiting situations" need to be asynchronous
①Timed tasks: setTimeout, setInterval ②Network
request: Ajax request, dynamic img loading ③Event
binding (addEventListener)
④Promise in ES6

1.6 5) Understand when the statement is placed in the asynchronous queue

Set time passed

2 Q&A

*What is the difference between synchronous and asynchronous? Give an example of synchronization and asynchronous respectively
* A written test question about setTimeout
* What are the scenarios where the front end uses asynchronous (same knowledge point 1.5)
* The principle of asynchronous implementation, the interaction between js and the kernel

2.1 What is the difference between synchronous and asynchronous? Give an example of synchronous and asynchronous respectively


①Synchronization will block code execution, but asynchronous will not; ②alert is synchronous, setTimeout is asynchronous

2.2 A written test question about setTimeout (relationship between asynchronous and single thread)

Because of the single-threaded feature of JS, asynchrony can solve the problem of blocking

2.3 What are the scenarios where the front end uses asynchronous

(Same knowledge point 1.5)

2.4 The principle of asynchronous implementation, the interaction between js and the kernel

(The principle of asynchrony-answer the js operating mechanism [single thread, task queue, event loop, asynchronous task queue statement])

js is single-threaded and can only do one thing at a time. In response to this feature, asynchrony can solve the problem of blocking.
In the task queue, the priority of synchronous events is higher than that of asynchronous events.
Synchronous tasks are put into the running stack, while asynchronous tasks are not put into the asynchronous task queue after the set time has passed. After the
synchronous task is executed, the tasks in the asynchronous task queue are put into the running stack, and the running stack is empty after running. Then go to the asynchronous task queue to monitor whether there is a task,
and then execute it again. Such a loop is an event loop (Event Loop).
[In fact, let’s just talk about it. Because of the single-threaded nature of js, asynchronous events will be executed after synchronous events during the event loop.]

Guess you like

Origin blog.csdn.net/weixin_37877794/article/details/114198890