A brief talk on the synchronization and asynchronous _javascript techniques of JavaScript

Source: Internet
Author: User
Tags message queue script tag

1. Hand-painted a picture description.

2. Why JavaScript is single-threaded (quote Nanyi teacher here)

A single thread of JavaScript, which is related to its purpose.

As a browser scripting language, the main purpose of JavaScript is to interact with the user and manipulate the DOM.

This determines that it can only be single-threaded, or it can create complex synchronization problems.

For example, assuming that JavaScript has two threads at the same time, one thread adds content to one of the DOM nodes, and another thread deletes the node, which thread should the browser use?

Therefore, in order to avoid complexity, from the birth, JavaScript is a single thread, which has become the core characteristics of the language, will not change in the future.

To take advantage of the computational power of multi-core CPUs, HTML5 proposes web worker standards that allow JavaScript scripts to create multiple threads, but the child threads are completely controlled by the main thread and must not manipulate the DOM.

So, the new standard doesn't change the nature of JavaScript single-threaded threads.

Portal: JavaScript operation mechanism of the event loop (events Loop) detailed

Where is the 3.JavaScript asynchronous representation?

Like the beginning of the figure, the person thinks the left main thread is synchronous, the left event queue (Message queue) is asynchronous.

Of course, there are many asynchrony in javascript:

Ajax (XMLHttpRequest)
Image tag,script tag,iframe (principle similar)
settimeout/setinterval
CSS3 transition/ Animation
postMessage
Web Workers
Web Sockets and more
...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.