javascript - Using jQuery $.when and $.then with two functions -


i know can use $.when().then() if have $.ajax call in $.when method. thing i'm trying use these methods while call function in $.when method , function in $.then method. function call in $.when method calls $.ajax method.

my problem won't respond if try doing it. want in $.when method , continue $.then method.

my initial function (called when clicking button) is:

function hideform(elem) {     var valid = validateemailpass(elem);     var user, email;      if (valid) {         user_info_all.style.display = "none";         showmessage(regerrmsg, "נירשם, אנא המתן..");         var email = document.getelementbyid('<%= reg_email.clientid %>'), pass = document.getelementbyid('<%= reg_pass.clientid %>'), age = document.getelementbyid('<%= reg_age.clientid %>'),         username = document.getelementbyid('<%= reg_user.clientid %>');         $.ajax({             type: "post",             url: 'register.aspx/register',             data: '{user: "' + username.value + '", pass: "' + pass.value + '", age: "' + age.value + '", mail: "' + email.value + '"}',             contenttype: "application/json; charset=utf-8",             datatype: "json",             success: function (validated) {                 showmessage(regerrmsg, validated.d);                 if (validated.d.indexof('נרשמת בהצלחה') == -1) {                     user_info_all.style.display = "block";                 }             },             failure: function (e) {                 $(reg_user_v).toggleclass('validated', false);                 $(reg_user_v).toggleclass('invalid', false);                 $(reg_pass_v).toggleclass('validated', false);                 $(reg_pass_v).toggleclass('invalid', false);                 $(reg_email_v).toggleclass('validated', false);                 $(reg_email_v).toggleclass('invalid', false);                 $(reg_age_v).toggleclass('validated', false);                 $(reg_age_v).toggleclass('invalid', false);                 showmessage(regerrmsg, "אירעה שגיאה בעת ההרשמה, נסה שנית.");                 user_info_all.style.display = "block";             }         });         return true;     }     return false; } 

validateemailpass function calls multiple ajax requests, tried placing function in $.when , rest of function in $.then this:

function hideform(elem) {     var valid;     $.when(function() {          valid = validateemailpass(elem);      }).then(function() {         // rest of function here     }); } 

$.when works on promises. in case validateemailpass method should return deferred , hence must change logic following

  1. validateemailpass must return $.deferred instance.
  2. resolve/reject deferred instance inside validateemailpass method once validations succeeded or failed. if expect then part should not called in case of validation failure, should reject deferred.

quoting jquery.when documentation

if single argument passed jquery.when() , not deferred or promise, treated resolved deferred , donecallbacks attached executed immediately.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -