jQuery的同步非阻塞Ajax调用 [英] jQuery synchronous non-blocking ajax calls
问题描述
在我的web应用程序,我有一系列的迅速解雇了Ajax调用。在服务器上,这些需要,因为它们是从客户端发送相同的顺序进行处理。
In my web application, I have a series of ajax calls that are fired rapidly. On the server, these need to be processed in the same order as they are sent from the client.
我一直在使用异步:jQuery的假
配置做到这一点。然而,这使GUI变得非常缓慢的,而它的块进行通话结束。随着异步:真正的
图形用户界面响应,但要求并不总是处理,以
I have been using the async: false
configuration in jQuery to do this. However this causes the GUI to become very sluggish while it blocks for calls to finish. With async: true
the GUI is responsive, but the requests are not always processed in order.
是否有其他的非阻塞的方式来排队Ajax请求,那么下一个是previous一个人完成后,只派?
Is there an alternative non-blocking way to queue up ajax requests, so the next one is sent only after the previous one has finished?
注:我没有请求处理的清单。在飞行中产生的请求,所以我需要能够塞住他们成某种产生时,他们一个FIFO队列的,那么消耗的一些过程的队列。
NOTE: I don't have a "list" of requests to process. The requests are generated on the fly, so I need to be able to stuff them into some sort of a FIFO queue when they are generated, then consume the queue with some process.
推荐答案
它可以很容易地用的 jQuery的承诺:
function firstAjax() {
return $.ajax({...});
}
function secondAjax() {
return $.ajax({...});
}
firstAjax().pipe(secondAjax).pipe(...).done(function() {
alert('all requests have successfully finished');
});
或
$.when(firstAjax()).pipe(secondAjax).pipe(...).done(function() {
alert('all requests have successfully finished');
});
http://jsfiddle.net/zerkms/WQcVD/1/
这篇关于jQuery的同步非阻塞Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!