Analysis of async:false/true function in Ajax request _ajax related

Source: Internet
Author: User

In this paper, we analyze the role of Async:false/true in AJAX requests. Share to everyone for your reference, specific 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, (default is True);

As above: False for synchronization, the AJAX request in this Testasync () method locks up the entire browser, and only after tet.php execution is finished can other operations be performed.

When Async:true, the AJAX request is asynchronous. But one of the problems: the AJAX request in Testasync () and its subsequent operations are performed asynchronously, so that when the tet.php is not finished, the operation behind the AJAX request may have been performed.

such as: alert (temp+ ' end ');

However, the temp data is only assigned after the AJAX request success, and the output is empty.

For more information on AJAX-related content readers can view the site topics: "JQuery Ajax Usage Summary", "JavaScript Ajax Operating Skills summary", "Php+ajax Techniques and Application Summary" and "ASP.net Ajax skills Summary of the topic"

I hope this article will help you with Ajax programming.

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.