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

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -