使用AJAX请求进行异步阵列更新
我一直在试图找出如何使用AJAX请求更新全局变量。我陷入困境,需要帮助使用AJAX请求进行异步阵列更新,ajax,asynchronous,global,Ajax,Asynchronous,Global,我一直在试图找出如何使用AJAX请求更新全局变量。我陷入困境,需要帮助 var markerArray = []; function JSONload(position){ //Loads JSON and markers console.log("data getting jsoned"); $.getJSON('json/***.json', function(result){ //gets the json and parses it into results $.each(re
var markerArray = [];
function JSONload(position){ //Loads JSON and markers
console.log("data getting jsoned");
$.getJSON('json/***.json', function(result){ //gets the json and parses it into results
$.each(result, function(index, value){ //for each result, give the index and the value
reps.push({value: value, index: index}); //push the value at to the array. ex. Ian Calderon
});
try{
for (var i=0; i<reps.length; i++){
if(reps[i].value.lat !== 'undefined' && reps[i].value.position === position){
var marker = (createMarker(reps[i].value.lat, reps[i].value.long, reps[i].index)); //only try to create the marker if there is the values
updateArray(markerArray, marker);
}}}
catch(e){
}
});
但坦率地说,我不知道如何继续我认为功能范围让你感到困惑 试试这个:
var that = this;
console.log("data getting jsoned");
updateArray(that.markerArray, marker);
该变量应该可以访问函数中的全局变量
var that = this;
console.log("data getting jsoned");
updateArray(that.markerArray, marker);