What's the best way to have a JS function to do a simple AJAX true/false request and return that result.
I want to do a simple AJAX request, in a JS function, that checks if a users session is still active. The function should wait for the AJAX request to complete and return true or false.
I've achieved this using a setInterval
but I don't think this is the best way. I tried to use jQuery $.when
but I couldn't get the function to return a result.
function checkSessionIsStillActive(){
var result; //ajax result will be put in here. It will be undefined until ajax complete
$.getJSON('application/app_cfc/session_check.cfm/ajax')
.done(function( data ) {
// return the value
result = data.session_active;
});
// Do an interval loop check until the result is defined
var interval_check = setInterval(function(){
// Return the result as soon as we have it
if(result != undefined ){
// Clear this interval loop
clearInterval(interval_check);
// return the result to the calling function
return result;
}
}, 100);
}