javascript - How to wait until the completion of asynchronous steps? -
i have form need run number of validation actions before submitting. these actions involve asynchronous steps ajax calls.
how can tell form wait until validation actions have been completed before submitting?
my validation function looks (pseudo-code):
function presubmit() { // validate field 1 ajax({ ... onsuccess:... }); // validate field 2 ajax({ ... onsuccess:... }); // fields 3, 4, etc. }
my current issue form gets submitted before onsuccess callbacks completed.
i of course of study create ajax calls synchronous, trying avoid because of performance impact.
if you're using jquery, might want consider jquery.deferred or promise
you phone call each ajax request (or function contains request), when previous phone call finish (successfully or failed).
if utilize $.when can manage failed request .then
callback. if utilize promises can create utilize .done
.fail
, .always
callbacks in various scenarios.
$.when($.ajax("...."), $.ajax("....")) .then(...);
resources:
http://joseoncode.com/2011/09/26/a-walkthrough-jquery-deferred-and-promise/ http://api.jquery.com/jquery.when/ http://api.jquery.com/promise/ javascript forms validation submit
No comments:
Post a Comment