javascript - jQuery.each() and Array Manipulation -
here's code:
$(document).ready(function() { var myarray = []; $.getjson("some url",function(data){ $.each(data, function(){ myarray.push("a string" + this); }); alert(myarray); }); //alert(myarray); });
the code shown working fine , displays array , contents.
however, when try display array having command line right after $.each
code block (commented out in sample code), array , contents not displayed. empty/ blank message returned instead.
why happening , how can fix it? i'd have command "alert(myarray);
" right after $.each
block.
thank in advance!
var myarray = []; var jqxhr = $.getjson( "some url", function(data) { $.each(data, function(){ myarray.push("a string" + this); }); }) ; jqxhr.complete(function() { console.log(myarray); });
jquery xhr object, or "jqxhr," returned $.getjson()
when request complete, .complete() callback fired immediately.
Comments
Post a Comment