The settings on the object window, only get "fixed" if I call the function[ctrUpadateCount] under an alert, if I do a direct call it wont work.
Any idea why? I had the same issue on another project. And had to apply a solution which cannot be applied here.
I’ve tried to explain the code bellow…
p.s.: sorry for bad English.
function ctrUpdateCount(idCtrForm)
{
$.ajaxSetup({ assync: false});
tableName= 'clients';
p_comando="select count(*) total from "+tableName;
$.post("execute.php", {comando : p_comando}, function(json){
v_total=json[1].total ; // <= this retuns 3
window['ctrBuffer_'+idCtrForm].count=v_total;
alert("count inside = "+ window['ctrBuffer_'+idCtrForm].count );//this alerts 3
},'json');
}
jQuery.fn.extend( { ctrLoad: function()
{
if( $(this).get(0).tagName =='FORM'){
idCtrForm=$(this).attr('id');
alert("direct count ="+ctrUpdateCount(idCtrForm) ); // this will alert 3
// and so the next alert
//teste=ctrUpdateCount(idCtrForm); // but if I use this,
//the next alert will show "undefined"
alert("count after = "+ window['ctrBuffer_'+idCtrForm].count );
}
}
})
Your “ctrUpdateCount” function is using
$.post(), which is asynchronous. It’s impossible to have the function return a value determined by the result of that asynchronous operation; it’s inherently nonsensical.I really can’t tell what you’re trying to do, but basically you have to do what you need to do with the results of the “POST” operation inside the callback function to
$.post().