Home  >  Article  >  Web Front-end  >  Introduction to the asynchronous running mechanism of JavaScript

Introduction to the asynchronous running mechanism of JavaScript

高洛峰
高洛峰Original
2017-03-19 16:19:051255browse

----The asynchronous running mechanism is as follows:

1. The left and right synchronization tasks are executed on the main thread, forming an execution stack

2. In addition to the main thread value, there is also Task queue, as long as the asynchronous task has a running result, an event is placed in the task queue

3. Once all the synchronous tasks in the execution stack are completed, the system will read the task queue and corresponding to the task to be executed The task is placed in the main thread. The main thread ends waiting for state , enters the execution stack, and starts execution

(As long as the main thread is empty, the task queue will be read, this is JavaScript running mechanism, this process is repeated)

4. The main thread repeats the above steps

----Why JavaScript is single-threaded:

1.JavaScript You can only do one thing at a time. As a browser scripting language, operations performed in multiple threads may cause conflicts, so JavaScript can only be single-threaded

----HTML5 The standard stipulates that the minimum value (shortest interval) of the second parameter of setTimeout() must not be less than 4 milliseconds. If it is lower than this value, it will automatically increase. Prior to this, older browsers set the minimum interval to 10 milliseconds. In addition, those DOM changes (especially those involving page re-rendering) are usually not executed immediately, but every 16 milliseconds. At this time, the effect of using requestAnimationFrame() is better than setTimeout().

It should be noted that setTimeout() only inserts the event into the "task queue". The main thread must wait until the current code (execution stack) is finished executing before the main thread executes its designated callback function. If the current code takes a long time, it may take a long time, so there is no way to guarantee that the callback function will be executed at the time specified by setTimeout().

----

"Task queue" is an event queue (can also be understood as a message queue). When the IO device completes a task, it is added to the "task queue" An event indicating that related asynchronous tasks can enter the "execution stack". The main thread reads the "task queue", which means reading the events in it.

The events in the "Task Queue", in addition to IO device events, also include some user-generated events (such as mouse clicks, page scrolling, etc.). As long as the callback function is specified, these events will enter the "task queue" when they occur, waiting for the main thread to read.

The so-called "callback function" (callback) is the code that will be hung up by the main thread. Asynchronous tasks must specify a callback function. When the main thread starts executing an asynchronous task, the corresponding callback function is executed.

"Task queue" is a first-in, first-out data structure. The events ranked first are read by the main thread first. The reading process of the main thread is basically automatic. As soon as the execution stack is cleared, the first event on the "task queue" will automatically enter the main thread. However, due to the "Timer" function mentioned later, the main thread must first check the execution time. Certain events can only return to the main thread after the specified time.

----JavaScript is executed immediately when there is no code in it. It is executed as soon as possible when the process is idle

The above is the detailed content of Introduction to the asynchronous running mechanism of JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn