In the course of JavaScript learning, synchronization and Asynchronization are two very troublesome concepts, especially for beginners. In short, when two or more things happen at the same time, this is called synchronization; when they do not happen at the same time, this is called asynchronous. In the course of JavaScript learning, synchronization and Asynchronization are two very troublesome concepts, especially for beginners. In short, when two or more things happen at the same time, this is called synchronization; when they do not happen at the same time, this is called asynchronous.
Although these two concepts seem simple, it takes some effort to understand them. We need to know what is synchronization and what is asynchronous through actual operations.
You may think that common JavaScript Functions are synchronized. When you are using setTimeout () and AJAX, you may also think they are synchronous, right? If I tell you that these two functions can also be asynchronous at some time, do you believe it?
To clarify the cause, we need Mr X to help.
Scenario 1: Mr X tries to use Synchronization
Condition:
1Mr X is a person who can solve complex problems and can execute any assigned tasks.
2. contact him by phone.
3 No matter what problems or tasks you encounter, you have to call Mr X for help.
4Mr X will provide you with an answer, or complete the task immediately, and notify you after the completion.
5. With the help of Mr X, you have completed the task and then went out to the movies.
In this process, what you have done with Mr X is synchronous communication.
When you ask a question, he is listening; when he gives the answer, you are also listening.
When a message is added to the queue, it waits until the message framework at the front of the Call Stack is processed. After the previous message is processed, event-loop removes it from the queue and adds the framework of the current message to the call stack.
This message starts again and instructs the call stack to clear its corresponding framework and then remove it from the queue.
Take a look at the following code:
function foo(){}function bar(){ foo();}function baz(){ bar();}baz();
The running function is baz () in the last line of the code snippet. It is added to the queue as a message. When event-loop selects it, the call stack starts the framework of the stack baz (), bar (), AND foo () during execution.
Remember, function calls in JavaScript are never completed directly, but completed through messages.
What are the specific asynchronous methods?
So far, I have come into contact with some APIs, such as setTimeout () and AJAX, which are specially identified as Asynchronous methods. What's going on?
One very important thing is to understand that things happen synchronously and that things happen asynchronously. With the help of events and event-loop, JavaScript can process messages asynchronously, but this does not mean that everything in JavaScript is asynchronous.
As I said before, messages will not leave the queue before calling the stack clearing framework. For example, you won't go out to watch a movie before you get the answer. What happens at this time is synchronous: you are standing there waiting for the task to be completed, you can see that the task is complete before leaving.
The above is the synchronous and asynchronous content of JavaScript. For more information, see PHP Chinese website (www.php1.cn )!