2013-09-24 57 views
0

我一直在試圖弄清楚如何使用AJAX請求更新全局變量。我卡住了,需要一些幫助。使用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); 
    } 
} 
    ); 

但坦率地說我不知道​​如何着手

回答

0

我覺得功能範圍扔你。

試試這個:

var that = this; 
console.log("data getting jsoned"); 

    updateArray(that.markerArray, marker); 

的變量應該有訪問您的全局變量的函數。