How can I pass a variable that has been calculated in the success callback of an ajax function, to the document.ready ?
I have this code
function loadActions()
{
var countDiv;
$.ajax({
type: "POST",
cache: false,
url:"modules/actions/actions.php",
success : function (data) {
$("#actions-container").html(data);
$('.action-details').hide();
countDiv = $('.action-tab-odd').length + $('.action-tab-even').length ;
}
});
return countDiv;
}
$(document).ready(function(){
var count = loadActions();
});
But count is allways undefined. How can I get this to work?
Thank you
You can’t –
$.ajaxis asynchronous.That means your code after the call of
$.ajaxwill be processed even if the call you’re making take a long time.Returning something is obviously completely not a thing that can be done.
You could however call a callbackfunction in the
successonce its done with your value so it’ll be handled. Or use it directly in yoursuccessmethod. If you need your data directly, just don’t use$.ajaxand create the right code server side.