Home >Web Front-end >Front-end Q&A >What does javascript synchronization mean?

What does javascript synchronization mean?

WBOY
WBOYOriginal
2022-01-19 16:29:071948browse

In JavaScript, synchronization means that the next task is executed after the previous task is completed. The program execution order is consistent with the order of the tasks. A major feature of the JS language is that single thread, at the same time You can only do one thing, which means that you can't move on to the next thing until one thing is finished.

What does javascript synchronization mean?

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

What does javascript synchronization mean?

One of the major features of S language is that it is single-threaded and can only do one thing at the same time. It means that after one thing is finished, the next thing can be done.

HTML5 proposes the Web Worker standard, allowing JavaScript scripts to create multiple threads. So there are synchronous and asynchronous.

Synchronization:

The next task is executed after the previous task is completed. The program execution sequence is consistent and synchronized with the task arrangement sequence.

Asynchronous:

Can handle other events while doing one thing.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>同步和异步</title>
</head>
<body>
    <script>
        console.log(&#39;第一条语句&#39;);
        setTimeout(function(){
            console.log(&#39;第二条语句&#39;);
        },5000)
        console.log(&#39;第三条语句&#39;);
    </script>
</body>
</html>

When outputting the results, the first statement is displayed first, then the third statement, and the second statement is displayed after waiting for 5 seconds.

The difference between synchronous and asynchronous is that the execution order of each process on the pipeline is different

When the timer time is 0, what kind of printing order will it be? Change the timer time of the above program to 0, execute the program, and find that the output sequence has not changed.

JS divides all tasks into two categories, synchronous tasks and asynchronous tasks

Synchronous tasks are executed on the main thread, forming an execution stack; asynchronous tasks are implemented through callback functions of.

There are three types of common asynchronous tasks:

Ordinary events: such as click, resize, etc.

Resource loading, such as load, error, etc.

Timers, including setInterval, setTimeout, etc.

Execute the synchronous tasks in the execution stack first, and put the asynchronous tasks into the task queue. Once all the synchronous tasks in the execution stack are completed, the system will read the asynchronous tasks in the task queue in order, so The asynchronous task being read ends the waiting state and enters the execution stack.

Related recommendations: javascript learning tutorial

The above is the detailed content of What does javascript synchronization mean?. 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