javascript - How to make JS function wait for JSON data to load? -


this works:

function getdatajson() {     var querystring = "https://www.examplesite.com/somejson.json";      $.getjson(querystring, function(data) {         dostuffwithdata(data)     });  }   function dostuffwithdata(json) {      // code refers json  }   getdatajson(); 

but complains variable (json) undefined somewhere in dostuffwithdata():

function getdatajson(callback) {     // gets share data , runs callback when received     var querystring = "https://www.examplesite.com/somejson.json";      $.getjson(querystring, function(data) {         if(typeof callback === "function") {             callback(data);         }     });  }   function dostuffwithdata(json) {      // code refers json  }  getdatajson(dostuffwithdata()); 

what doing wrong? $.getjson() call takes second or 2 need wait , stuff after it. think issue code execution order, i've misunderstood how pass data callback function.

it better if load data variable other functions can access.

this:

getdatajson(dostuffwithdata()); 

should this:

getdatajson(dostuffwithdata); 

otherwise invoked function , attempts pass result of function getdatajson


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -