Ajax中 async的作用
2015-12-16 10:00
471 查看
test.html
<a href="javascript:void(0)" onmouseover="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
<?php
echo
"here is html code";
sleep(5);
?>
async: false,(默认是true);
如上:false为同步,这个 testAsync()方法中的Ajax请求将整个浏览器锁死,
只有tet.php执行结束后,才可以执行其它操作。
当async: true 时,ajax请求是异步的。但是其中有个问题:testAsync()中的ajax请求和其后面的操作是异步执行的,那么当tet.php还未执行完,就可能已经执行了 ajax请求后面的操作,
如: alert(temp+' end');
然而,temp这个数据是在ajax请求success后才赋值的,结果,输出时会为空。
<a href="javascript:void(0)" onmouseover="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
<?php
echo
"here is html code";
sleep(5);
?>
async: false,(默认是true);
如上:false为同步,这个 testAsync()方法中的Ajax请求将整个浏览器锁死,
只有tet.php执行结束后,才可以执行其它操作。
当async: true 时,ajax请求是异步的。但是其中有个问题:testAsync()中的ajax请求和其后面的操作是异步执行的,那么当tet.php还未执行完,就可能已经执行了 ajax请求后面的操作,
如: alert(temp+' end');
然而,temp这个数据是在ajax请求success后才赋值的,结果,输出时会为空。
相关文章推荐
- 【详解】简单验证码的解析
- Android通过startService实现文件批量下载
- OC_04_03
- vaadin系列(一)框架和spring整合
- 这是什么原因啊
- MFC关于VS2013内存释放的定位
- android TextView中间删除线显示
- Solr配置与简单Demo
- OC_04_02
- javascript函数式编程要掌握的知识点讲解
- OC学习篇之---Foundation框架中的NSArray类和NSMutableArray类
- 万年历---java版
- VNCServer 配置
- 使用visio 提示此UML形状所在的绘图页不是UML模型图的一部分 请问这个问题怎么解决?
- OC_04_01
- [No000063]Python格式化输出
- UICollectionView详解
- 视频时间戳转换
- MXNet设计和实现简介 a short introduction to mxnet design and implementation
- jQuery插件之ajaxFileUpload