微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ajax 同步异步

转自
http://blog.csdn.net/gaoyusi4964238/article/details/4378987

这里首先引用$.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块后面的脚本(另一个线程)例:
[javascript] view plaincopy
$.ajax({
type:"POST",
url:"Venue.aspx?act=init",255);"> dataType:"html",255);"> success:function(result){ //function1()
f1();
f2();
}
failure:function (result) {
alert('Failed');
},255);"> function2();
在上例中,当ajax块发出请求后,他将停留function1(),等待server端的返回,但同时(在这个等待过程中),前台会去执行function2(),也就是说,在这个时候出现两个线程,我们这里暂且说为function1() 和function2()。
当把asyn设为false时,这时ajax的请求时同步的,也就是说,这个时候ajax块发出请求后,他会等待在function1()这个地方,不会去执行function2(),知道function1()部分执行完毕。
注:success中的方法f1(),f2()一般(即f1(),f2()不包括ajax块时)不会异步执行,就是说f2的执行是以f1()为前提的。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐