2014-03-25 58 views
0

我希望能夠擁有多個實時地圖。我有一個海軍報圖,一切都是通過這個功能實時更新工作:用於多個實時地圖的SetTimeout

 function updateOnce(plot) 
     { 
      plot.setData([{ label: 'G-force', data: getRandomData(), lines: { show: true } }]); 
      plot.setupGrid();//sensorPlots[i] 
      plot.draw(); 

      setTimeout(function() { updateOnce(plot); }, 30); 
    } 

現在我的問題是,我動態生成plot並將其添加到一個數組。我怎麼能設置這種類型的函數迭代每個數組,並對數組中的每個圖都運行此函數而不影響其他函數?

我已經試過這樣:

 function updateOnce() 
     { 
      $.each(sensorPlots, function (i, val) { 
       val.setData([{ label: 'G-force', data: getRandomData(), lines: { show: true } }]); 
       val.setupGrid();//sensorPlots[i] 
       val.draw(); 
       setTimeout(updateOnce, 30); 
      }); 

     } 

sensorPlots is an array filled with `plots` 

但隨後這將導致我的圖形與每plot我加移動速度越來越快。

回答

1

問題似乎是,每次運行超時函數時,都會爲每個繪圖聲明一個新的超時值。這將導致被稱爲與指數增加頻率的功能,這是非常糟糕的......如果你做這樣的內容:

function updateOnce() { 
    $.each(sensorPlots, function (i, val) { 
     val.setData([{ label: 'G-force', data: getRandomData(), lines: { show: true}}]); 
     val.setupGrid();//sensorPlots[i] 
     val.draw(); 
    }); 
    setTimeout(updateOnce, 30);   
} 

updateOnce(); 
+0

我不明白'setTimeout'不夠好,所以這可能是爲什麼我掙扎。此外,我只是試過這個解決方案,但它似乎功能neever被稱爲 – user1084319

+0

我很抱歉。我昨天回答時顯然很累。你仍然需要在更新函數中使用setTimeout,但不是在每個循環中。這樣,每次更新函數運行時,您將設置一個新的超時,而不是每個繪圖一個。我已經更新了我的答案。 – EasyPush