javascript - Load JSON with AJAX to be used in another function -


i've got 2 functions, 1 called button triggered, other 1 when "process" triggered button finished. to load json data ajax request in calculateprize() function reduce loading time @ end of function finish()

function calculateprize() {     // ...some code goes here     // load ajax data here , parse in "finish function" }  function finish() {     // retrieve win     $.getjson("/generate").done(function(data){         // console.log(data.state)         // set win amount         amount_field.append( document.createtextnode(data.win_amount));     }); } 

how in best way?

you can call calculateprize function ajax done - , pass data parameter

function calculateprize(responsdata) {      console.log(responsedata); }  function finish() {     // retrieve win     $.getjson("/generate").done(function(data){         // call function - pass data parameter        calculateprize(data);      }); } 

or

store data in global variable , access in calculateprize function

    var responsedata = null;      function calculateprize() {          console.log(responsedata);     }      function finish() {         // retrieve win         $.getjson("/generate").done(function(data){             responsedata = data;             // call function             calculateprize();          });     } 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -