Before we talk about the cycle of events , Let's talk about some basic knowledge first , It is convenient for students to make the whole event cycle clearer , More deeply connected .
although JS The engine is single-threaded , But its host environment （ The browser or node） It's multithreaded , That is, there are other threads in the browser kernel that can be used to assist JS Execution of engine threads .
Here are some browser threads , Students can have a general understanding of ：
JS Engine threads , We can also call it the main thread , Its main function is to run JS Synchronization code , give an example ：
var a = 1 //1 console.log() //2 setTimeout() //3 ajax() //4 Copy code
among 1,2 Line code is synchronous code , Execute directly in the main thread , The first 3,4 Line code is an asynchronous function , Assigned by the main thread to the timer trigger thread and http Asynchronous threads are two threads that process asynchronous code
The timer trigger thread we mentioned earlier , Browser event threads and http Asynchronous threads are all asynchronous operation processes , These processes mainly deal with asynchronous tasks assigned by the main thread , These asynchronous tasks thrown by the main thread will specify their own callback functions , When the asynchronous operation process finishes executing the task , Will send the returned callback function to the task queue .
We can think of task queue as a static queue storage structure （ fifo ）, This storage structure stores some callback functions thrown by asynchronous operation process （ After the asynchronous task is completed , Just throw its callback function in the task queue . For example, the browser event process needs to handle a click event , This event has a callback function , It's after the click event is complete , The browser event process sends the bound callback function to the task queue ）
Event loop It can be understood as a task or work , The main function is to detect the task queue and the execution stack generated by the main thread , Once the code in the execution stack is executed , The callback function is automatically extracted from the task queue .
Event loop It can also be understood as a mechanism or operation mode , Can be ： The main thread assigns asynchronous tasks to asynchronous operation threads , After the asynchronous operation thread completes the task, it allocates the callback function to the task queue , The task queue holds the callback function , Then, when the main thread finishes executing the code, it will extract the callback function in the task queue . This cycle is called Event loop
Everyone has his own way of understanding and thinking , No matter how you understand memory , My article makes students understand Event loop How it works .
In our actual development, we may not need to understand Evnet loop The impact on asynchronous operations , But after we have learned and understood it to a certain extent , We'll do a script with asynchronous operations / There is a clearer understanding of the execution of functions , More clearly it is in the implementation of what kind of position and state