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.
在我的web应用程序中,我有一系列快速启动的ajax调用。在服务器上,需要按照从客户端发送的顺序来处理这些数据。
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.
我一直在使用jQuery中的async: false配置来实现这一点。然而,这会导致GUI在阻塞调用完成时变得非常缓慢。使用async: true, GUI是响应的,但是请求并不总是按顺序处理。
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?
有没有另一种非阻塞的方式来排队ajax请求,以便下一个请求只在前一个请求完成之后才发送?
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.
注意:我没有要处理的请求的“列表”。请求是动态生成的,所以我需要能够在生成的时候将它们填充到某种类型的FIFO队列中,然后使用一些进程来消耗队列。
1 个解决方案
#1
6
It can be done easily with jQuery promises:
jQuery承诺很容易做到:
function firstAjax() {
return $.ajax({...});
}
function secondAjax() {
return $.ajax({...});
}
firstAjax().pipe(secondAjax).pipe(...).done(function() {
alert('all requests have successfully finished');
});
or
或
$.when(firstAjax()).pipe(secondAjax).pipe(...).done(function() {
alert('all requests have successfully finished');
});
http://jsfiddle.net/zerkms/WQcVD/1/
http://jsfiddle.net/zerkms/WQcVD/1/
#1
6
It can be done easily with jQuery promises:
jQuery承诺很容易做到:
function firstAjax() {
return $.ajax({...});
}
function secondAjax() {
return $.ajax({...});
}
firstAjax().pipe(secondAjax).pipe(...).done(function() {
alert('all requests have successfully finished');
});
or
或
$.when(firstAjax()).pipe(secondAjax).pipe(...).done(function() {
alert('all requests have successfully finished');
});
http://jsfiddle.net/zerkms/WQcVD/1/
http://jsfiddle.net/zerkms/WQcVD/1/