Differences between async attribute values in ajax: synchronous and asynchronous, and asynchronous. ajaxasync

Source: Internet
Author: User

Differences between async attribute values in ajax: synchronous and asynchronous, and asynchronous. ajaxasync

In jquery, the ajax method has a property async used to control synchronization and Asynchronization. The default value is true, that is, ajax requests are asynchronous requests by default, and sometimes AJAX synchronization is used in projects. This synchronization means that when JavaScript code is loaded to the current AJAX, all the code on the page will be stopped and the page will be suspended, after AJAX is executed, other code pages will continue to be run and the suspended state will be removed. Asynchronous mode can run the same as other codes when the AJAX code is running.

In ajax, The async attribute is used to control the way data is requested. The default value is true, that is, the data is requested asynchronously by default.

1. The async value is true (asynchronous)

After ajax sends a request, the foreground will continue to execute the script after the ajax block while waiting for the server to return the returned result, until the server returns the correct result to execute success, that is to say, two threads are executed at this time. After the ajax block sends a request, one thread and the script after the ajax block (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 above example, when the ajax block sends a request, it will stop at function1 (), waiting for the server to return, but at the same time (in this waiting process ), the foreground will execute function2 ().

2. The async value is false (synchronous)

When the current AJAX is executed, the subsequent JavaScript code is stopped until the AJAX execution is complete.

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');       },  }  function2(); 

When asyn is set to false, ajax requests are synchronized. That is to say, after the ajax block sends a request, it will wait at function1, function2 () is not executed until the function1 () part is completed.

Differences between Ajax synchronization and Asynchronization

Var returnValue = null; xmlhttp = createXmlHttp (); xmlhttp. onreadystatechange = function () {if (xmlhttp. readyState = 4 & xmlhttp. status = 200) {if (xmlhttp. responseText = "true") {returnValue = "true" ;}else {returnValue = "false" ;}}; xmlhttp. open ("Post", url, true); // asynchronously transmits xmlhttp. setRequestHeader ("If-Modified-Since", "0"); // Ajaxxmlhttp is not cached. send (sendStr); return returnValue;

The xmlHttpReq. onreadystatechange status value can be used only in asynchronous mode! The following are different asynchronous and synchronous call methods:

Java

XmlHttpReq. open ("GET", url, true); // asynchronous xmlHttpReq. onreadystatechange = showResult; // showResult is the name of the callback function xmlHttpReq. send (null); function showResult () {if (xmlHttpReq. readyState = 4) {if (xmlHttpReq. status = 200 ){******}}}

Java

XmlHttpReq. open ("GET", url, false); // synchronous xmlHttpReq. send (null); showResult (); // although showResult is the callback function name, its usage is different ~ Function showResult () {// if (xmlHttpReq. readyState = 4) {You don't need to use it here. Directly dosomething ~ // If (xmlHttpReq. status = 200) {****** // dosomething //} xmlhttp. open ("Post", url, true );

If it is synchronous (false), the return value is true or false. Because onreadystatechange is executed after sending, the program waits until onreadystatechange is executed, after responseText is obtained, the next statement is executed. Therefore, returnValue must have a value.

If it is asynchronous (true), the return value must be null, because the program continues to execute the next statement after sending does not wait for the xmlhttp response, therefore, null is returned if returnValue is not changed.

If you want to obtain the xmlhttp return value, you must use synchronization. the return value cannot be obtained asynchronously.

When using the xmlhttp pool synchronously and asynchronously, note that only xmlhttp can be created when xmlhttp is obtained, and the used xmlhttp cannot be retrieved from the pool, because the readyState of the used xmlhttp is 4, therefore, both synchronous and asynchronous requests send but onreadystatechange is not executed.

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.