jquery - Question about JavaScript variable scope -
jquery - Question about JavaScript variable scope -
can tell me why alert empty?
var pending_dates = []; $.getjson('/ajax/event-json-output.php', function(data) { $.each(data, function(key, val) { pending_dates.push({'event_date' : val.event_date}); }); }); alert(pending_dates);
i can't head around this. not declaring pending_dates
global variable, accessible within each loop? how 1 solve this?
please note json output working well. if declare pending dates within getjson function
(and alert within function), works, need store info in array outside of getjson
function.
thanks contributions.
edit
thanks comments code working:
pending_dates = []; $.getjson('/ajax/event-json-output.php', function(data) { $.each(data, function(key, val) { pending_dates.push({'event_date' : val.event_date}); }); }).success(function() { alert(pending_dates); })
thanks lot contributions!
i think problem $.getjson
asynchronous phone call - returns , alert(pending_dates)
invoked.
however, when happens, response asynchronous phone call may not have been received, , hence pending_dates
may not have been populated.
that why empty @ time alert(pending_dates)
called.
javascript jquery variables scope each
Comments
Post a Comment