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.