我有一個運行代碼來初始化一個抽象圖形包。在創建圖形實例後,我從服務器獲取獲取請求的數據,並想更新圖形數據提供者。問題是,有時(對於IE6-8),保存數據提供者的對象尚未初始化,所以當我嘗試更新數據時,JavaScript崩潰。JavaScript:保證對象初始化
在對象準備就緒之前,如何才能執行代碼的延遲? 僞:
...
...
...
// Init code
$graph = new Graph();
...
...
...
// GET request
$.getJSON(..., ..., function(data) {
...
...
...
// Make sure that $graph.series[0] is ready
// Should not use while, but something similar in functionality
while (!($graph.series && $graph.series[0]))
; // Sleep the code until object is ready
// Set the dataprovider after init complete
$graph.series[0].setData(data);
...
...
...
});
...
...
...
問候