JS Asynchronous model

Source: Internet
Author: User

Although the article on the JS asynchronous model is quite a lot, the front end has their own understanding of it. The purpose of this article is to comb my understanding of the JS Async model and to provide a reference for other front ends in this regard. This article only talk about the JS asynchronous model, the next article to pull promise/a Standard, jquery promise and deferred objects. This article is mainly about the browser-side JS asynchronous. Here's a list of information about this: JavaScript Asynchronous Programming: Designing a fast-responding Web application (this book is one of the few books dedicated to JS async), someone else writes about JS Async (JS Asynchronous Programming 1,js Asynchronous Programming 2), the browser internals (this article is very long, I only looked at the previous part). There are also some similar articles I will not list.

JS's async is completely different from Java's async. The asynchronous of Java is the simultaneous operation of multiple threads, and the access of synchronous resources is realized by synchronous lock. And JS asynchronous is event-based, multiple threads to execute event queue Plug executable event to achieve asynchronous, this way to ensure that only one thread at a time to access synchronization resources. First (pure hand-painted, slightly ugly):

The events here can be understood as a piece of code, usually in the form of a callback function. The event handler continuously checks the event queue for an executable event and executes the event if there is one. These executable events are generated by two threads, a time thread, and an I/O thread. The Settimeout,setinterval function sets the time thread to plug events into the event queue at a later point or at several times. Ajax,dom Event Registration, worker registration, PostMessage, etc., will be registered on the I/O thread, allowing it to throw events to the event queue when a behavior is triggered. Here's an example.

First create the aysnc.html with the following code:

<div id= ' domaysnc ' >test</div>          <script type= "text/javascript"  src= ' aysnc.js ' ></ Script>            </body>

The code for Aysnc.js is as follows:

var Dom=document.getelementbyid (' Domaysnc ');d om.onclick=function () {    console.log (' I/O event ');}; Window.settimeout (function () {    console.log (' time event ');},0);

To access aysnc.html This resource, the browser will load aysnc.js this file and give it to the JS engine to interpret and execute. When executing dom.onclick= .... This statement, the I/O thread is registered with an event that triggers the I/O thread to plug into the event queue when the DOM is clicked. This triggering process is different from calling Dom.click (), which is the difference between asynchronous and synchronous. When executing window.settimeout ... When this statement is made, the time thread is registered with an event, 0 This argument indicates that the event is immediately plugged into the event queue, and actually the timeline generates into an event (and the polling of the event handler) takes time, and the fastest engine is about 6ms.

Here's a look at some of the window.settimeout applications. The first is the animation, under the non-modern browser (including IE9), Animation is to use window.settimeout or window.setinterval to achieve, the big changes are cut into small changes, and then with a small time (in jquery, this very small time is 13ms. ) to perform these small changes. This allows the JS engine to generate, execute, and update the UI. Transition, which is supported under modern browsers, can specify a transition effect for a property. He is not involved in JS execution, so the efficiency will be much higher.

Window.settimeout can also be used for large-task splits. For example, in the JS processing a large array, if placed in an event processing, it will block the event handler. Window.settimeout (function () {\* is split into small task *\},2), and in a similar way, if another event is triggered, it can be executed.

JS Asynchronous model

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.