Analysis of the Role of async: false/true in Ajax requests, ajaxasync

Source: Internet
Author: User

Analysis of the Role of async: false/true in Ajax requests, ajaxasync

This document analyzes the functions of async: false/true in Ajax requests. We will share this with you for your reference. The details are as follows:

Test.html code:

<A href = "javascript: void (0)" onmouseover = "testAsync ()">

Asy. js code:

Function testAsync () {var temp; $. ajax ({async: false, type: "GET", url: 'tet. php ', complete: function (msg) {alert ('complete') ;}, success: function (data) {alert ('success '); temp = data ;}}); alert (temp + 'end ');}

Tet. php code:

<? Php echo "here is html code"; sleep (5);?>

Async: false (true by default );

As shown above: false indicates synchronization. The Ajax request in the testAsync () method will lock the entire browser. Other operations can be performed only after the execution of tet. php is complete.

When async: true, ajax requests are asynchronous. However, there is a problem: ajax requests in testAsync () and subsequent operations are executed asynchronously, so when tet. php may have executed the operations following the ajax request,

For example, alert (temp + 'end ');

However, the temp data is assigned a value only after the ajax request success. The result is null in the output.

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.