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

java - Solr query version issue: Invalid version or the data in not in 'javabin' format -

Hard vs. Soft Water: What's The Difference?

The Ten Most Livable Cities In The World