2011-12-07 150 views
0

在我一直在工作的主要應用程序中,並且Stack Overflow的專家已經成爲救生員,我決定避免一些我見過的異步併發症,將主程序的SortArray分支拆分爲它自己的功能。測試在函數返回這些結果之前顯示完美的結果。爲什麼我的數組「未定義?」

這樣可以節省我一些處理的方式是我可以在之前對數組進行排序,然後將其發送到主處理函數中。這樣,所有數組在整個ProcessArray函數中的處理都是相同的。

在下面的代碼示例中,我顯示了當前的結構,這是我給出的建議(最近由jfriend00提供)的解釋。我做了兩個$ .getJSON調用,第一個是成功子句中的第二個,並且在我結束第一個getJSON調用之前(此時我已經測試並驗證了在此處未顯示的處理過程中創建的數組),我調用SortArray在其中一個數組上,然後將它傳遞給將結果發送到ProcessArray的例程。

儘管SortArray代碼本身在內成功,但通過此方法進入ProcessArray的數組被標記爲「未定義」。如果這不是異步處理的更多問題,那麼我認爲這是數組引用的問題。但我不知道如何解決它。

function buildTree(centralID) { 
    $(document).ready(function(){ 
    $.getJSON('MurakiMaida.json', function(data) { 
     $.each(data.person, function(i, xdata) { 
     ...create a small set of arrays 
     }); //end of first $.each() routine 
     $.getJSON('MurakiMaida.json', function(data) { 
      $.each(data.person, function(i, xdata) { 
      ...use an array just created to create another 
      }); //end of second each() 
     }); //end of second getJSON 
     $("#spchildren").html(ProcessArray(SortArray(childIDs, true), centralID, false)); 
    }); //end of first getJSON 
}); //document.ready 
} 
+0

你從'SortArray'返回什麼?並在'ProcessArray'中作爲參數?如果問題僅與這些方法有關,那麼如果我們看不到方法,我們可以通過什麼機制來解決問題?我假設沒有依賴於第二個JSON調用,這將不會返回。 –

+0

除了你似乎兩次加載完全相同的內容(相同的JSON文件,爲什麼不只是迭代接收到的數據兩次?)之外,你在嵌套的回調函數中重複使用相同的變量名稱。雖然不一定如此,但這是在尋求麻煩。 – Bart

+0

假設childIDs是在$ .getJSON調用之一的成功時創建的,它仍然是像2天前那樣的問題,並由Larry K在下面提到:http://stackoverflow.com/questions/8377344/scope-of-javascript-array -differs-from-ie9-to-firefox-chrome –

回答

1

如果我正確理解你的代碼(一個大的if),這是一個異步處理的問題。你的第二個Ajax調用的結果func正在創建「child」數組,對嗎?

但是Ajax調用的結果func在稍後纔會調用。您正在調用ProcessArray函數。

添加 也許這將幫助:

你的代碼(如果我的理解對不對)是完全一樣的,只是下面我命名的功能,而不是內聯定義它們:

var async_function_1 = function(data) { 
    $.each(data.person, function(i, xdata) { 
    ...create a small set of arrays 
    }); //end of first $.each() routine 

    $.getJSON('MurakiMaida.json', async_function_2); 

    $("#spchildren").html(ProcessArray(SortArray(childIDs, true), centralID, false)); 
    // See the problem? You are calling ProcessArray before async_function_2 
    // has a chance to run. 
} 

var async_function_2 = function(data) { 
    $.each(data.person, function(i, xdata) { 
    ...use an array just created to create another 
    }); //end of second each() 
}; 

function buildTree(centralID) { 
    $(document).ready(function(){ 
     $.getJSON('MurakiMaida.json', async_function_1); 
}); //document.ready 
} 

修復移動ProcessArray碼到第二異步函數定義的結尾如下:

# Same as above except 

var async_function_1 = function(data) { 
    $.each(data.person, function(i, xdata) { 
    ...create a small set of arrays 
    }); //end of first $.each() routine 

    $.getJSON('MurakiMaida.json', async_function_2); 
} 

var async_function_2 = function(data) { 
    $.each(data.person, function(i, xdata) { 
    ...use an array just created to create another 
    }); //end of second each() 
    $("#spchildren").html(ProcessArray(SortArray(childIDs, true), centralID, false)); 
}; 
+0

工程就像一個魅力,謝謝。對不起,我錯過了第二個實例的問題,但我要調查上面的建議,以確保我真的需要第二次getJSON調用。邁克爾 –