加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ajax请求中的async:false/true的作用

发布时间:2020-12-15 21:44:05 所属栏目:百科 来源:网络整理
导读:Ajax 作用有两个 1.局部刷新 2.异步 。 在项目中经常可以看到 async 设置为false的。 有时候异步会出问题。 test.html a href="javascript:void(0)" onmouSEOver="testAsync()" asy.js function testAsync(){ var temp; $.ajax({ async: false, type : "GET"

Ajax 作用有两个 1.局部刷新 2.异步 。 在项目中经常可以看到 async 设置为false的。 有时候异步会出问题。

test.html


<a href="javascript:void(0)" onmouSEOver="testAsync()">

asy.js
function testAsync(){
var temp;
$.ajax({
async: false,
type : "GET",43); font-family:Arial; font-size:14px; line-height:26px">url : 'tet.php',43); font-family:Arial; font-size:14px; line-height:26px">complete: function(msg){
alert('complete');
},43); font-family:Arial; font-size:14px; line-height:26px">success : function(data) {
alert('success');
temp=data;
}
});
alert(temp+' end');
tet.php
<?php
echo "here is html code";
sleep(5);
?>
如上:false为同步,这个 testAsync()方法中的Ajax请求将整个浏览器锁死,
只有tet.php执行结束后,才可以执行其它操作。
当async: true 时,ajax请求是异步的。但是其中有个问题:testAsync()中的ajax请求和其后面的操作是异步执行的,那么当tet.php还未执行完,就可能已经执行了 ajax请求后面的操作,
如: alert(temp+'

然而,temp这个数据是在ajax请求success后才赋值的,结果,输出时会为空。

引用:http://blog.csdn.net/yanfangphp/article/details/8261333

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读