从名为 bunchOfData 的数组中加载一堆 json 文件。 .url 是每个 json 文件的 url。
如何在 processData 函数中读取我的变量“myI”?
for(var i = 0; i < bunchOfData.length; i++){
$.getJSON(bunchOfData[i].url, {"myI":i}, function(ds){}).success(processData);
function processData(ds){
console.log(ds.myI); //undefined
}
}
最佳答案
function successHandler(i){
console.log('i is instantiated/stored in the closure scope:',i);
return function(data, textStatus, jqXHR){
// here, the server response and a reference to 'i'
console.log('i from the closure scope:',i, 'other values',data,textStatus,jqXHR);
}
}
var i = 0; i < bunchOfData.length; i++){
$.getJSON(bunchOfData[i].url, {"myI":i}, function(ds){}).success(successHandler(i));
}
关于javascript - 带有数据变量的 json 请求 - 如何读取函数中的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15281421/