javascript - Assign a value to global variable inside a function -


i have problem assigning value global variable , using inside of function. here code:

var chartinfo = {"c0":"0", "c1":"0"}; // global variable $.getjson("http://127.0.0.1:8080/chartinfo", function(json1){     chartinfo = json1; // need assign json1's value chartinfo }); $(function () {     $(document).ready(function() {         alert("external chartinfo " + chartinfo.c0.name); // need use chartinfo here 

the alert fails because request not finished yet. try this:

var chartinfo = {     "c0": "0",     "c1": "0" };  var jqxhr = $.getjson("http://127.0.0.1:8080/chartinfo", function (json1) {     console.log("success"); })     .done(function () {     chartinfo = json1;     alert("external chartinfo " + chartinfo.c0.name); // need use chartinfo here })     .fail(function () {     console.log("error"); }) 

http://jsfiddle.net/zpusm/


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -