Ajax请求中的async:false/true的作用
2016-03-24 10:41
501 查看
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后才赋值的,结果,输出时会为空。
来源:网络
相关文章推荐
- poj-2299-Ultra-QuickSort
- 【转】UI设计师可不知的安卓屏幕知识(dpi,ppi)
- 用 xib / Storyboard 创建 UITableView 的 header 和 footer
- iOS开发UISearchBar的使用及searchBar的成员属性解释
- UIImagePickerController 介绍
- UIStackView
- stringbuffer与stringbuilder的区别?
- OUI-10133:登台区无效
- UE4 含源码插件合集
- LINQ中AsEnumerable与AsQueryable的区别
- 用UE4源码工作之 常用链接合集
- 读视频文件和运动物体检测cvCaptureFromCAM cvQueryFrame
- druid的安装
- compileSdkVersion与buildToolsVersion
- ueditor div style被过滤 解决办法
- 《iOS Human Interface Guidelines》——Info Button
- requirejs 小结
- UISplitViewController - 分割视图控制器
- poj-2533-Longest Ordered Subsequence
- 《iOS Human Interface Guidelines》——Detail Disclosure Button