在循环中使用setInterval来更改间隔

时间:2015-10-12 06:43:28

标签: javascript timer setinterval openlayers-3

我在第二个答案中使用了这个技术:

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 个答案:

答案 0 :(得分:1)

    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;

您可以在此处计算transitionTime。

        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);// <----------------------------->Not redefined
        };

在这里你使用与上面相同的transitionTime,它不会被重新加工! 为什么不,这不是错误,而是......

我不认为你的问题来自计时器,而是来自你的参数。

这里有一个片段,看你的代码有关时间和间隔没有问题: 我刚刚删除了所有不关心&#39;计时&#39;和间隔。

&#13;
&#13;
var log = function(val){
    console.log(val);
    document.getElementById('el').innerHTML+='<div><pre>' + val + '</pre><div>';
  }

var timer ,
    i = 1 , 
    length = 5 ,
    transitionTime = 200 ;



        timer = setInterval(function() {
          log('first timerId : ' + timer);
          
            segmentConstruction()
        
        }, transitionTime );

        function segmentConstruction(multipointCoords, tracksTime) {
          log(' \tsegmentConstruction : i = ' + i + ' / ' + length);
        
          //if (i === length - 1) {
          if (i >= length - 1) {
                clearInterval(timer);
                log('\t\twe finish with : i = ' + i + ' / ' + length);
            } else {
                i++;
                clearInterval(timer);
                timer = setInterval(function() {
                    log('loop timerId : ' + timer);
                    segmentConstruction();
                }, transitionTime);
            };
        };
&#13;
<div id='el'></div>
&#13;
&#13;
&#13;