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.