Async in JavaScript

Source: Internet
Author: User
Tags setinterval

In C#,java, asynchronous methods, usually accompanied by terms such as multithreading, concurrency, and so on, such as the Async method in C #, are run on a thread pool, and a callback function notifies the main thread after the asynchronous method has finished running.

So because JavaScript is single-threaded, how does it explain async?

First, the Async method is given a definition of what method is an asynchronous method. I think there are 2 main points of asynchronous methods, one is not blocking the execution of the current code, and the other is a callback method. That is, the main thread can be notified when the Async method finishes running.

In fact, when it comes to JavaScript asynchrony, don't forget that there are 2 kinds of async in JavaScript, one is browser-based asynchronous Io, like the nature of Ajax. The other is asynchronous based on timing methods settimeout and SetInterval.

For asynchronous Io, such as Ajax, the code is executed sequentially, but when the request is actually processed, a separate browser thread is processed and the callback is triggered when the processing is complete. In this case, there are actually 2 thread principals involved in the asynchronous process, one is the primary thread of JavaScript and the other is the browser thread.

People familiar with JavaScript know that there is an event queue inside JavaScript, all the processing methods are in this queue, the JavaScript main thread is polling this queue processing, the advantage is that the CPU is always busy state. This mechanism is the same as the message pump in Windows, and is driven by messages (events). For a description of this message queue, refer to the http://www.iamued.com/qianduan/1645.html article.

In JavaScript, "Do not block" and "have callbacks" can also be implemented through the SetTimeout function. For example, the following code:

function F1 (callback) {  setTimeout () task code           for function () {//F1 Callback ();   );} F1 (F2);

In this way, JavaScript executes the F2 method when it calls the F1 method, putting the real logic into 1 seconds, and after running through the logical logic. Careful analysis, in fact, this so-called non-blocking is actually just blocking the delay. In a single-threaded environment, and not really asynchronous. In fact, the settimeout method for time accuracy is very poor, and can not really guarantee is executed in 1 seconds, if there is a long time in the event queue method is running, then blocked the real F1 method content operation, until a long time to allow the completion of the method. When the real logic of F1 starts to run, the rest of the code waits until it finishes running.

For example, the following code:

SetTimeout (function() {alert (' do ');},0); var i=0;  while (i<100000) {  console.log (i);  I+ +;}

The alert will not run immediately, but will wait until the while run is complete before alert. According to the book "JavaScript Asynchronous Programming," the settimeout and setinterval delay minimum interval is 4ms,quora also has summed up http://www.quora.com/ How-accurate-are-javascript-timers.

Another way to implement Asynchrony in JavaScript is that the Web Worker,web Worker is a JavaScript running in the background, independent of other scripts, without affecting the performance of the page. Web workers relies on browsers, currently supported by Lsafari, Chrome, Opera, Internet Explorer (version 10) and Mozilla Firefox. Once you use the Web Workers is equivalent to using multithreaded technology. For more details on web workers, you can refer to the document http://www.w3school.com.cn/html5/html_5_webworkers.asp online.

Http://www.cnblogs.com/feng_013/archive/2011/09/20/2175007.html

This article is from "a blog" blog, provenance http://cnn237111.blog.51cto.com/2359144/1556987

Async in JavaScript

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.