我使用的技術在這裏的第二個答案:在循環中使用的setInterval改變間隔
Changing the interval of SetInterval while it's running
,但在區間的變化沒有被設置。代碼也在使用OpenLayers 3:
var coordinate,
i = 1,
length = multipointCoords[0].length;
var currentTime = tracksTime[0];
var nextTime = tracksTime[1];
speedOption = 100; // the highter this value, the faster the tracks, see next line
var transitionTime = (nextTime - currentTime)/speedOption;
var timer;
timer = setInterval(function() {
segmentConstruction(multipointCoords, tracksTime);
}, transitionTime);
function segmentConstruction(multipointCoords, tracksTime) {
coordinate = ol.proj.fromLonLat(multipointCoords[0][i]);
lineString.appendCoordinate(coordinate);
if (i === length - 1) {
clearInterval(timer);
} else {
i++;
map.addLayer(trackLayer);
clearInterval(timer);
currentTime = tracksTime[i];
nextTime = tracksTime[i + 1];
timer = setInterval(function() {
segmentConstruction(multipointCoords);
}, transitionTime);
};
};
我在做什麼錯?
謝謝。
當你清楚你的間隔是當你需要創建一個新的。這也是當你需要重新計算你的區間值的時候,否則它會以相同的速度運行。目前,如果'我===長度 - 1'的間隔將停止,就是這樣。我認爲你想在那一點上改變間隔速度? – ste2425
哦,我應該澄清。當我===長度爲1時,循環應該結束。線串將完成。這是else語句中不起作用的代碼。基本上,我逐段渲染矢量線(線串)。 – interwebjill
我本可以寫成 'if(i
interwebjill