Async attribute values in Ajax are synchronous and asynchronous, and synchronous and asynchronous differences _ajax related

Source: Internet
Author: User

The Ajax method in jquery has a property async used to control synchronization and Asynchrony, which is true by default, that is, Ajax requests are asynchronous, and sometimes Ajax synchronization is used in the project. This synchronization means that when the JS code is loaded into the current Ajax will put all the code in the page to stop loading, the page appears suspended animation state, when this Ajax execution will continue to run the other code page suspended animation state. Asynchronous, the Ajax code runs the same way that other code can.

This property is async in Ajax to control the way the data is requested, by default, true, which is to request data asynchronously by default.

One, async value is true (asynchronous)

When Ajax sends a request, the foreground will continue to execute the script behind the Ajax block as it waits for the server side to return, until the server side returns the correct result to execute the success, which means the two threads are executing, and the AJAX block makes the request after a thread and Ajax blocks behind the script (another thread)

For example

$.ajax ({  
     type: "POST", 
     URL: "Venue.aspx?act=init", 
      dataType: "html", 
     success:function (Result) {  //function1 ()
       F1 (); 
       F2 ();  
    } 
     Failure:function (Result) {  
      alert (' Failed ');  
     }, 
 } 
 function2 ();

In the example above, when the Ajax block makes a request, he stays function1 (), waits for the server-side return, but at the same time (during the wait), the foreground executes function2 ().

Second, async value is False (sync)

When the implementation of the current AJAX will stop the execution of the following JS code, until the completion of Ajax execution, can continue to execute the following JS code.

For example

$.ajax ({  
     type: "POST", 
     URL: "Venue.aspx?act=init", 
     dataType: "html", 
     Async:false,
    success: function (Result) {  //function1 ()
       F1 (); 
       F2 (); 
     } 
    Failure:function (Result) {  
      alert (' Failed ');  
     } 
  
 

When the Asyn is set to False, the AJAX request is synchronized, that is, when the Ajax block makes a request, he waits in the function1 () and does not perform the function2 () until Function1 () part of the execution is completed.

The difference between Ajax synchronization and Asynchrony

var returnvalue = null; 
XMLHTTP = Createxmlhttp (); 
Xmlhttp.onreadystatechange = function () { 
  if (xmlhttp.readystate = = 4 && xmlhttp.status =) { 
    if (XMLH Ttp.responsetext = = "true" { 
      returnvalue = "true"; 
    } 
    else { 
      returnvalue = "false"; 
    }}} 
; 
Xmlhttp.open ("Post", url,true); Asynchronous transfer 
Xmlhttp.setrequestheader ("if-modified-since", "0");//Do not cache Ajax
Xmlhttp.send (SENDSTR); 
Return returnvalue;

You can use the Xmlhttpreq.onreadystatechange state value only when you are asynchronous! The following are different ways to invoke asynchronous and synchronous:

Java

Xmlhttpreq.open ("Get", url,true);//asynchronous mode
  xmlhttpreq.onreadystatechange = Showresult;//showresult is a callback function name
  Xmlhttpreq.send (null);
function Showresult () {  
  if (xmlhttpreq.readystate = 4) {   
   if (Xmlhttpreq.status =) {
  }
}

Java

Xmlhttpreq.open ("Get", url,false);//sync mode  
      xmlhttpreq.send (null);  
      Showresult (); Showresult Although it is a callback function name but the specific usage is not the same ~  
function Showresult () {   
       //if (xmlhttpreq.readystate = = 4) {This is not  necessary, Direct dosomething ~  
        //if (xmlhttpreq.status =) {  
          ******//dosomething  
        //}  
      //}  
}
Xmlhttp.open ("Post", url,true);

If it is synchronous (false), the return value is true or false because after the send, the execution of the onreadystatechange begins, and the program waits until the onreadystatechange is finished. After you get responsetext, you will continue to execute the next statement, so returnvalue must have a value.

If it is asynchronous (true), the return value must be NULL, because the program executes the next statement without the XMLHTTP response after send, so that the returnvalue has not come and the changes have returned null.

All if you want to obtain the XMLHTTP return value must be synchronized, the return value cannot be obtained asynchronously.

When using the XMLHTTP pool synchronously, be aware that you can only create a new XMLHTTP when you get XMLHTTP, and you cannot take out the used XMLHTTP from the pool because the XMLHTTP of the used readystate is 4, So synchronous asynchronous will send but not execute onreadystatechange.

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.