问题描述
这可能是一个微不足道的问题,但我想知道是否有办法以某种方式知道当最后一个Ajax调用被完成。因此,可以说我有3异步Ajax调用
It may be a trivial question, but I am wondering if there is way to somehow know when the last ajax call gets completed. So lets say I have 3 asynchronous ajax calls
$.ajax({
type: "GET",
datatype: "json",
url: <my service url 1>
})
.done(function() {
// handler
});
$.ajax({
type: "GET",
datatype: "json",
url: <my service url 2>
})
.done(function() {
// handler
});
$.ajax({
type: "GET",
datatype: "json",
url: <my service url 3>
})
.done(function() {
// handler
});
我要显示一个进度条,当第一个电话开始,并把它隐藏在最后一个结束。问题是,即使我打电话给他们的顺序,因为调用是异步的,我不知道如何告诉当所有呼叫完成。我可以嵌套这些人在里面另外一个,但据我了解这将需要更长的时间为你将不得不等待另一个到结束。有没有办法以某种方式同步呢?
I want to show a progress bar when the first call starts and hide it when the last one finish. The issue is that even though I call them in sequence, because calls are asynchronous, I don't know how to tell when all calls finish. I could nest them one inside another, but then as far as I understand it will take much longer as one will have to wait for another to finish. Is there a way to sync it somehow?
推荐答案
我以前也有同样的需求。如果你可以使用jQuery,看看有:http://lostechies.com/joshuaflanagan/2011/10/20/coordinating-multiple-ajax-requests-with-jquery-when/
I used to have the same need. If you can use jQuery, have a look there : http://lostechies.com/joshuaflanagan/2011/10/20/coordinating-multiple-ajax-requests-with-jquery-when/
否则,您可以通过AJAX调用,回来在你的进度指示器更新在每个异步处理的最后通过一个简单的回调函数。
Otherwise, you can pass a simple callback function through your AJAX call that comes back in your progress indicator update at the end of each async treatment.
这篇关于有没有一种方法来ajax调用同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!