javascript - Fire ajax call after all the previous ajax calls have completed -
lets have functions like:
function a(){ func b(); ajax call last; } func b(){ if condition1 func c(); if condition2 func d(); if condition3 func e(); } func c(){ ajax cal 1 } func d(){ ajax call 2 } func e(){ ajax call 3 }
my problem ajax call last should fired when previous ajax calls have completed .i.e ajax call 1 , 2 , 3.
if you're using jquery have access various calls based on promise. calls $.ajax
return promise can used defer subsequent calls. see documentation jquery when
call (docs here).
using when
can execute function after $.ajax
call has completed. can use same technique single such call $.ajax
, or multiple calls.
i'm not sure based on question how want chain calls, here's attempt based on code (untested should on right track):
func b() { var callc = null; var calld = null; var calle = null; if (condition1) callc = $.ajax("/call/1"); if (condition2) calld = $.ajax("/call/2"); if (condition3) calle = $.ajax("/call/3"); $.when(callc, calld, calle).then($.ajax("/call/last")); }
.then
execute once 3 ajax calls have completed, regardless of success or failure. @ .done
if want continue if calls return success.
Comments
Post a Comment