Home >Web Front-end >JS Tutorial >Analysis of the role of async:false/true in Ajax requests
This article mainly introduces the role of async:false/true in Ajax requests. It explains the specific functions and usage skills of async:false/true in Ajax requests based on the analysis of examples. Friends in need can refer to it
The example of this article analyzes the role of async:false/true in Ajax requests. Share it with everyone 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, (default is true) ;
As above: false means synchronization. The Ajax request in this testAsync() method locks the entire browser. Only after the execution of tet.php is completed, other operations can be performed. When async: true, the ajax request is asynchronous. But there is a problem: the ajax request in testAsync() and the subsequent operations are executed asynchronously, so when tet.php has not been executed, the operations following the ajax request may have been executed. For example:alert(temp ' end');
However, the temp data is assigned after the ajax request success, and as a result, it will be empty when output. The above is what I compiled for everyone. I hope it will be helpful to everyone in the future. Related articles:Ajax realizes three-level cascading of provinces and municipalities
Ajax simple implementation of uploading pictures and previewing them
Simple application based on Ajax form submission and background processing
##
The above is the detailed content of Analysis of the role of async:false/true in Ajax requests. For more information, please follow other related articles on the PHP Chinese website!