jquery - Multiple REST calls - best practise -


i following:

1.multiple call single rest service 2.store received data in array 3.when data received, iterate throug array filter , display data.

i have following code (not exact copy, pseudocode :))

for(var = 0; < numberofrestcalls; i++){    $.ajax(       url: 'url'       success: function(result){          storedatainarray();       }    ) }  (var j = 0; j < array.length; j++){    if (array[j] == something)       displaydata(array[j]); } 

but happens, not data loaded. because ajax async, second cycle iterated before data loaded. so, how can wait rest calls complete? ty

you can take advantage of $.when() , deferreds this. don't know in order ajax calls going complete time can act on results when have completed. following should work

var ajaxcalls = [];  for(var = 0; < numberofrestcalls; i++){    ajaxcalls.push($.ajax({ url: 'url' })); }  $.when.apply($, ajaxcalls).done(function() {      // arguments here arrays ajax requests in     // ajaxcalls, each array looks [ data , statustext, jqxhr ]     var args = array.prototype.slice.call(arguments, 0);      for(var = 0; < args.length; i++){        storedatainarray(args[0])     }      // results storedatainarray() function calls  }); 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -