Waiting For Ajax Responses To Fill Object
Solution 1:
As it's probably the answer, I'll post it here. I think you're looking for:
http://api.jquery.com/ajaxComplete/
You can use this to find out when all of your ajax has completed and then do something with the response.
Solution 2:
OK I figured it out. Because ajaxComplete
is launching after every single AJAX request as I said before I had to create function that will wait until all request have been done. So I did it like this:
I changed my function:
var response = {}
for (var i = 0; i < length; i++){
$.ajax({
url : url[i],
dataType : 'json'
}).success(function(result) {
processResult(result);
})
}
to:
response = {}
for (var i = 0; i < length; i++){
callAJAX(url[i]);
}
_wait4ajax();
function callAJAX is like this:
functioncallAJAX(url){
$.ajax({
url : url[i],
dataType : 'json'
}).success(function(result) {
processResult(result);
})
}
and _wait4ajax
is function where I check if all properties of object aren't undefined
so:
(I've got lists of properties which should be filled in object - this property is in visibleLayers
array
function_wait4ajax(){
var controlArray = [], self = this;
var length = this.visibleLayers.length;
for (var i = 0; i < length; i++) {
if (this.cachedFeatures[this.visibleLayers[i]] === undefined) {
controlArray.push('false');
} else {
controlArray.push('true');
}
}
if (controlArray.indexOf('false') != -1) {
setTimeout(function() {
self._wait4ajaxComplete();
}, 50);
} else {
//AJAX has ended - Object is ready
}
}
I messed up my pseudocode (created for this site purpose) with actual code from my project, so it will probably doesn't work in this form, but I want to specify specific idea of this subject. fill free to edit this post. :)
Solution 3:
The answer for this question will do what you want:
How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?
Your code:
var response = {}
for (var i = 0; i < length; i++){
$.ajax({
url : url[i],
dataType : 'json',
async: false,
success: function(result) {
processResult(result);
}
}
Post a Comment for "Waiting For Ajax Responses To Fill Object"