Don't feel dumb
Asynchronous operations puzzle most people when first confronted with them.
Since you're using jQuery, you could use deferred objects to handle AJAX responses. The $.ajax() method itself returns a deferred object, so rather than your function returning true/false you could just return the AJAX object itself and use the done/fail methods that will fire when the AJAX call completes:
Since you're using jQuery, you could use deferred objects to handle AJAX responses. The $.ajax() method itself returns a deferred object, so rather than your function returning true/false you could just return the AJAX object itself and use the done/fail methods that will fire when the AJAX call completes:
Code:
function ajaxEmail(address) {
"use strict";
var postdata;
postdata = { email: address };
return $.ajax({
type: 'POST',
url: ajaxpath,
data: postdata,
dataType: 'text'
});
}
ajaxEmail(emailaddress)
.done(function(text) {
if (text === 'invalid') {
emailFocus('You must enter a valid Email address.');
} else if (text === 'used') {
emailFocus('The e-mail address you entered is already in use.');
}
})
.fail(function () {
window.alert('I had trouble communicating with the server. Please try again later.');
});