(我不知道,你鏈接的圖形資格作爲一個密集的任務,但不管)
我已經打破了使用超時的任務得到了較好的效果。假設你正在做這樣的事情:
var largeSelection = d3.selectAll('svg circle')
.data(aReallyLargeDataset);// Expensive Bind Operation
largeSelection.enter()
.append('circle')// Lots of appending
.attr('r', function() { /* expensive calculations */ return ... });
largeSelection// Many refreshes
.attr('cx', function() { /* more expensive calculations */ return ... });
這可能需要在瀏覽器1秒渲染(很長一段時間,考慮到一切都將在此任務期間被凍結)。你可以通過這樣分解來改善它:
setTimeout(function() {
var largeSelection = d3.selectAll('svg circle')
.data(aReallyLargeDataset);// Expensive Bind Operation
setTimeout(function() {
largeSelection.enter()
.append('circle')// Lots of appending
.attr('r', function() { /* expensive calculations */ return ... });
setTimeout(function() {
largeSelection// Many refreshes
.attr('cx', function() { /* more expensive calculations */ return ... });
}, 100);
}, 100);
}, 100);
對不起,討厭的嵌套和超時。您可以以更具可讀性/可擴展性的方式重構/抽象它。無論如何,以這種方式分解任務使瀏覽器有機會「呼吸」並更新DOM,從用戶的角度看,應用程序似乎不會「卡住」。
如果仍然感覺呆滯,你可以打破上去更:
var entering = largeSelection.enter()
.append('circle');// Lots of appending
setTimeout(function() {
entering.attr('r', function() { /* expensive calculations */ return ... });
}, 100);
是我的回答有幫助? – meetamit