I'm making in code a few requests with JQuery and get
. It looks like:
我在代码中用JQuery做了一些请求并得到了。看起来像:
$.get('address1', function() { ... });
$.get('address2', function() { ... });
$.get('address3', function() { ... });
// This code should be runned when all 3 requests are finished
alert('Finished');
So, are there any ways to detect whether there is still processing request and run marked code only when all 3 requests are finished.
那么,是否有任何方法可以检测是否仍有处理请求并仅在所有3个请求完成时运行标记代码。
Thanks.
谢谢。
4 个解决方案
#1
13
You can make use of deferred objects [docs] introduced in jQuery 1.5:
您可以使用jQuery 1.5中引入的延迟对象[docs]:
$.when(
$.get('address1', function() { ... }),
$.get('address2', function() { ... }),
$.get('address3', function() { ... })
).then(function() {
alert('Finished');
});
Reference: jQuery.when
参考:jQuery.when
The jQuery learning center has a nice introduction to deferred objects / promises.
jQuery学习中心对延迟对象/承诺进行了很好的介绍。
#2
2
var isFinished = [];
$.get('address1', function() { isFinshed.push["address1"]; allDone(); });
$.get('address2', function() { isFinshed.push["address2"]; allDone(); });
$.get('address3', function() { isFinshed.push["address3"]; allDone();});
var allDone = function(){
if(isFinished.length < 3)return
alert('Finished');
};
#3
0
var fin1 = false;
var fin2 = false;
var fin3 = false;
$.ajax({
url: "address1",
success: function(){
fin1 = true;
fnUpdate();
}
});
$.ajax({
url: "address2",
success: function(){
fin2 = true;
fnUpdate();
}
});
$.ajax({
url: "address3",
success: function(){
fin3 = true;
fnUpdate();
}
});
function fnUpdate(){
if(fin1 && fin2 && fin3){
alert('fin');
}
}
#4
0
var count = 0;
$.get('address1', function() { count++; ... });
$.get('address2', function() { count++; ... });
$.get('address3', function() { count++; ... });
var int = setInterval(function() {
if (count === 3) {
clearInterval(int);
alert('done');
}
}, 10);
#1
13
You can make use of deferred objects [docs] introduced in jQuery 1.5:
您可以使用jQuery 1.5中引入的延迟对象[docs]:
$.when(
$.get('address1', function() { ... }),
$.get('address2', function() { ... }),
$.get('address3', function() { ... })
).then(function() {
alert('Finished');
});
Reference: jQuery.when
参考:jQuery.when
The jQuery learning center has a nice introduction to deferred objects / promises.
jQuery学习中心对延迟对象/承诺进行了很好的介绍。
#2
2
var isFinished = [];
$.get('address1', function() { isFinshed.push["address1"]; allDone(); });
$.get('address2', function() { isFinshed.push["address2"]; allDone(); });
$.get('address3', function() { isFinshed.push["address3"]; allDone();});
var allDone = function(){
if(isFinished.length < 3)return
alert('Finished');
};
#3
0
var fin1 = false;
var fin2 = false;
var fin3 = false;
$.ajax({
url: "address1",
success: function(){
fin1 = true;
fnUpdate();
}
});
$.ajax({
url: "address2",
success: function(){
fin2 = true;
fnUpdate();
}
});
$.ajax({
url: "address3",
success: function(){
fin3 = true;
fnUpdate();
}
});
function fnUpdate(){
if(fin1 && fin2 && fin3){
alert('fin');
}
}
#4
0
var count = 0;
$.get('address1', function() { count++; ... });
$.get('address2', function() { count++; ... });
$.get('address3', function() { count++; ... });
var int = setInterval(function() {
if (count === 3) {
clearInterval(int);
alert('done');
}
}, 10);