Ajax请求中的async:false/true的作用
生活随笔
收集整理的這篇文章主要介紹了
Ajax请求中的async:false/true的作用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
test.html
<a href="javascript:void(0)" οnmοuseοver="testAsync()">
asy.js
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
<?phpecho "here is html code";sleep(5);
?>
async: false,(默認(rèn)是true);
如上:false為同步,這個(gè) testAsync()方法中的Ajax請求將整個(gè)瀏覽器鎖死,
只有tet.php執(zhí)行結(jié)束后,才可以執(zhí)行其它操作。
當(dāng)async: true 時(shí),ajax請求是異步的。但是其中有個(gè)問題:testAsync()中的ajax請求和其后面的操作是異步執(zhí)行的,那么當(dāng)tet.php還未執(zhí)行完,就可能已經(jīng)執(zhí)行了 ajax請求后面的操作,
如: alert(temp+'???end');
然而,temp這個(gè)數(shù)據(jù)是在ajax請求success后才賦值的,結(jié)果,輸出時(shí)會(huì)為空。
來源:網(wǎng)絡(luò)
?
轉(zhuǎn)載于:https://www.cnblogs.com/ChinazhouWang/p/5314486.html
總結(jié)
以上是生活随笔為你收集整理的Ajax请求中的async:false/true的作用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Modular_exponentiati
- 下一篇: C++ 容器及选用总结