jquery的ajax同步和异步
2016-02-29 10:28
507 查看
之前一直在写JQUERY代码的时候遇到AJAX加载数据都需要考虑代码运行顺序问题。最近的项目用了到AJAX同步。这个同步的意思是当JS代码加载到当前AJAX的时候会把页面里所有的代码停止加载,页面出去假死状态,当这个AJAX执行完毕后才会继续运行其他代码页面假死状态解除。
而异步则这个AJAX代码运行中的时候其他代码一样可以运行。
jquery的async:false,这个属性
默认是true:异步,false:同步。
$.ajax({
type: "post",
url: "path",
cache:false,
async:false,
dataType: ($.browser.msie) ? "text" : "xml",
success: function(xmlobj){
}
});
有了这个属性可以相对的减少代码运行书序问题,但是如果用的太多,页面假死次数太多。这样反而导致用户体验不佳~!
$.Ajax()中 async 和success的官方的解释:
async
Boolean
Default: true
By default, all requests are sent asynchronous (e.g. this is set to true by default). If you need synchronous requests, set this option to false. Note that synchronous requests may temporarily lock the browser, disabling any actions
while the request is active.
success
Function
A function to be called if the request succeeds. The function gets passed two arguments: The data returned from the server, formatted according to the 'dataType' parameter, and a string describing the status. This is an Ajax
Event.
在这里,async默认的设置值为true,这种情况为异步方式,就是说当ajax发送请求后,在等待server端返回的这个过程中,前台会继续 执行ajax块后面的脚本,直到server端返回正确的结果才会去执行success,也就是说这时候执行的是两个线程,ajax块发出请求后一个线程 和ajax块后面的脚本(另一个线程)例:
$.ajax({
type:"POST",
url:"Venue.aspx?act=init",
dataType:"html",
success:function(result){ //function1()
f1();
f2();
}
failure:function (result) {
alert('Failed');
},
}
function2();
在上例中,当ajax块发出请求后,他将停留function1(),等待server端的返回,但同时(在这个等待过程中),前台会去执行function2(),也就是说,在这个时候出现两个线程,我们这里暂且说为function1() 和function2()。
当把asyn设为false时,这时ajax的请求时同步的,也就是说,这个时候ajax块发出请求后,他会等待在function1()这个地方,不会去执行function2(),知道function1()部分执行完毕。
转载至:http://www.cnblogs.com/xmphoenix/archive/2011/11/21/2
4000
257651.html
而异步则这个AJAX代码运行中的时候其他代码一样可以运行。
jquery的async:false,这个属性
默认是true:异步,false:同步。
$.ajax({
type: "post",
url: "path",
cache:false,
async:false,
dataType: ($.browser.msie) ? "text" : "xml",
success: function(xmlobj){
}
});
有了这个属性可以相对的减少代码运行书序问题,但是如果用的太多,页面假死次数太多。这样反而导致用户体验不佳~!
$.Ajax()中 async 和success的官方的解释:
async
Boolean
Default: true
By default, all requests are sent asynchronous (e.g. this is set to true by default). If you need synchronous requests, set this option to false. Note that synchronous requests may temporarily lock the browser, disabling any actions
while the request is active.
success
Function
A function to be called if the request succeeds. The function gets passed two arguments: The data returned from the server, formatted according to the 'dataType' parameter, and a string describing the status. This is an Ajax
Event.
在这里,async默认的设置值为true,这种情况为异步方式,就是说当ajax发送请求后,在等待server端返回的这个过程中,前台会继续 执行ajax块后面的脚本,直到server端返回正确的结果才会去执行success,也就是说这时候执行的是两个线程,ajax块发出请求后一个线程 和ajax块后面的脚本(另一个线程)例:
$.ajax({
type:"POST",
url:"Venue.aspx?act=init",
dataType:"html",
success:function(result){ //function1()
f1();
f2();
}
failure:function (result) {
alert('Failed');
},
}
function2();
在上例中,当ajax块发出请求后,他将停留function1(),等待server端的返回,但同时(在这个等待过程中),前台会去执行function2(),也就是说,在这个时候出现两个线程,我们这里暂且说为function1() 和function2()。
当把asyn设为false时,这时ajax的请求时同步的,也就是说,这个时候ajax块发出请求后,他会等待在function1()这个地方,不会去执行function2(),知道function1()部分执行完毕。
转载至:http://www.cnblogs.com/xmphoenix/archive/2011/11/21/2
4000
257651.html
相关文章推荐
- 转帖:解决jquery.js在myeclipse中报错的问题
- JQuery 绑定单击事件到某个函数的的方法
- jQuery EasyUI使用教程之启用数据网格内联编辑
- FF上使用jquery ajax时出现一个小问题
- Jquery easyui从零单排之datagrid整表编辑保存到数据库
- jQuery取消特定的click事件
- jquery trigger实现联动的方法
- jquery trigger函数执行两次的解决方法
- jQuery中通过ajax的get()函数读取页面的方法
- Jquery Validation 相关使用记录
- Jquery的DataTable插件 AJAX 服务器分页的的学习心得(java版)
- jQuery切换表单
- jQuery ajax实现三级联动
- jQuery实现表格td宽度等于th宽度
- jQuery判断某个元素是否存在
- jQuery(事件-页面事件ready(fn)) 3.0
- jquery(四)插件
- jQuery学习笔记01
- Jquery中关于动画的一些操作函数
- 放弃使用jQuery实现动画