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"); })
Comments
Post a Comment