This is an AJAX request and the A stands for Asynchronous, that means your code doesn't stop and wait for the response, unless you say so. $.get
is only a wrapper around $.ajax
. If you want the request to be synchronous, you need to use the $.ajax
instead like this:
var checkemail = 0;
$.ajax({
async: true,
url: "bla.aspx",
data: {day:"friday"},
dataType: text,
success: function(data){
console.log(data);
checkmail = data;
}
});
console.log("checkemail: " + checkemail);
if (checkemail == '1') { /* do something */ }
Or you can use the callback function to check the data. A callback function is the function you defined in the success attribute. This function gets called after the request is finished. So you are able to do something like this:
$.get("bla.aspx", {
day: "friday"
},
function(checkmail) {
console.log("checkemail: " + checkemail);
if (checkemail == '1') { /* do something */ }
}
);
if the /* do something */
is very long you can put it into a separate function and than put the function call to the get request like this:
var do_something = function(checkmail) {
console.log("checkemail: " + checkemail);
if (checkemail == '1') { /* do something */ }
}
$.get("bla.aspx", {
day: "friday"
},
do_something // note the missing `()`, it must be like this
);