Javascipt Source Parsing asynchronous

Source: Internet
Author: User

Our common asynchronous operations are:

Timer setTimeoutpostmessageWebWorkorCSS3 Animation XMLHttpRequestHTML5 Local data, etc...

JavaScript requires asynchronous communication when interacting with the server, just like Ajax. Because it is an asynchronous model, it is similar to Ajax when invoking the local data interface provided by the Transaction Navigator (here I assume), the browser itself has internal XHR method asynchronous processing, but at this time the JS code will be synchronous down execution, in fact, is a non-blocking code.

problem: because there is no blocking, the code will continue to execute after sending the AJAX request, then the subsequent operation if the data will be dependent on the error, so this will need to wait for the Ajax to return to perform subsequent operations.

Deferred

Deferred provides an abstract non-blocking solution (such as a response to an asynchronous request), and he creates a promise object that returns a response at some point in the future, simply as a processing scheme for an asynchronous/synchronous callback function.

$. Deferred is used within the jquery code with four modules, namely the Promise method, the DOM Ready,ajax module, and the animation module.

The transformation of Ajax

Traditional jquery's Ajax operations are traditionally written (before 1.5):

$.ajax ({

URL: "",

Success:function () {alert ("Success")},

Error:function () {alert ("fail")}

});

$.ajax () accepts an object parameter, which contains two methods, the Success method, which specifies the callback function after the operation succeeds, and the error method specifies the callback function after the operation failed.

Introduced with new deferred after version 1.5

$.ajax (""). Done (function () {alert ("Success")}). Fail (function () {alert ("fail")});

Javascipt Source Parsing 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.