【问题标题】:ASync Array update with AJAX request使用 AJAX 请求更新异步数组
【发布时间】:2013-09-24 19:41:31
【问题描述】:

我一直试图弄清楚如何使用 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(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){
   }
    });

}

我试过这样做:

function updateArray(array, obj){
$.ajax({
    success : function(){
        array.push(obj);
    }
}
    );

但坦率地说,我不确定如何继续

【问题讨论】:

    标签: ajax asynchronous global


    【解决方案1】:

    我认为函数作用域让你失望。

    试试这个:

     var that = this; 
     console.log("data getting jsoned");
    
      updateArray(that.markerArray, marker);
    

    该变量应该可以访问函数中的全局变量。

    【讨论】:

      猜你喜欢
      • 2020-05-03
      • 1970-01-01
      • 2023-04-01
      • 2016-10-22
      • 1970-01-01
      • 2013-05-24
      • 2011-10-13
      • 2020-09-10
      • 2010-11-06
      相关资源
      最近更新 更多