JavaScript synchronization vs. asynchronous

Source: Internet
Author: User
Tags script tag

1. Hand-painted a picture description.

2. Why JavaScript is a single thread (quoted here as Nanyi teacher)

The single thread of JavaScript is related to its purpose.

As a browser scripting language, JavaScript's primary purpose is to interact with the user and manipulate the DOM.

This determines that it can only be single-threaded, otherwise it can lead to 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 the other thread deletes the node, which thread should the browser take precedence over?

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

To take advantage of the computational power of multicore CPUs, HTML5 proposes a web worker standard that allows JavaScript scripts to create multiple threads, but the child threads are completely controlled by the main thread and must not manipulate the DOM.

So, this new standard does not change the nature of JavaScript single threading.

Portal: JavaScript operating mechanism in detail: Another talk about event Loop

What is the asynchronous representation of 3.JavaScript?

Like the beginning of the diagram, the individual thinks that the left main thread is synchronous, the left event queue (Message Queuing) is asynchronous.

Of course, there are many async in javascript:

    • Ajax (XMLHttpRequest)
    • Image Tag,script Tag,iframe (Similar principle)
    • Settimeout/setinterval
    • CSS3 transition/animation
    • PostMessage
    • Web Workers
    • Web Sockets
    • And more ...

Allen's blog summarizes several asynchronous ideas, portals: Asynchronous grooming in JavaScript (0)

JavaScript synchronous and asynchronous

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.