我有两个功能,一个是在按钮被触发时调用,另一个是在按钮触发的“进程”完成时调用。
我想从calculatePrize()
函数中的AJAX请求加载JSON数据,以减少函数结束时的加载时间Finish()
function calculatePrize() {
// ...some code goes here
// I would like to load the AJAX data already here and just parse it in the "finish Function"
}
function Finish() {
// retrieve win
$.getJSON("/generate").done(function(data){
// console.log(data.state)
// set the win amount
amount_field.append( document.createTextNode(data.win_amount));
});
}
如何以最好的方式这样做?
答案 0 :(得分:1)
您可以从Ajax Done调用calculatePrize
函数 - 并将数据作为参数传递
function calculatePrize(responsData) {
console.log(responseData);
}
function Finish() {
// retrieve win
$.getJSON("/generate").done(function(data){
// Call the function - pass the data as parameter
calculatePrize(data);
});
}
OR
将数据存储在全局变量中并访问calculatePrize
函数
var responseData = null;
function calculatePrize() {
console.log(responseData);
}
function Finish() {
// retrieve win
$.getJSON("/generate").done(function(data){
responseData = data;
// Call the function
calculatePrize();
});
}