如何在setInterval()中逐渐增加或减少间隔

时间:2018-12-09 17:17:19

标签: javascript setinterval intervals

let data = [
  ["48.85585695936588,2.317734961729684"],
  ["48.87234429654349,2.351466422300973"],
  ["48.85376742273335,2.3639977028185513"]
];

let points =  data.map(item => {
  var eachCoordinates = item[0].split(',');
  return{
    lat: eachCoordinates[0],
    lng: eachCoordinates[1]
  }
})

console.log(points);


// one line code, but two times we are calling the split functionality

let pointsOneLineCode =  data.map(item => ({
    lat: item[0].split(",")[0],
    lng: item[0].split(",")[1]
  })
)

console.log("one line code",pointsOneLineCode)

我的目标:逐渐将“ x”的值增加1(开始时,“ x”的增长缓慢,随着它的增加,它开始越来越快地增长)

5 个答案:

答案 0 :(得分:0)

使用setTimeout()来代替递归方法,每次都以较低的delay值调用自身。检查下一个示例:

var x = 0;
var time = 1000;

function Start()
{
    x += 1;
    time -= 50;
    console.log("x:" + x, "time:" + time);

    if (time > 0)
        setTimeout(Start, time);
}

// Initialize the recursive calls.

Start();

答案 1 :(得分:0)

一旦确定了计时器的时间,就无法更改它。解决方案是使用setTimeout而不是setInterval,以便您可以建立具有新延迟的新计时器。然后,每个连续的计时器可以使用不同的时间:

var x = 100;      // Display data
var int = 1000;   // Initial delay
var timer = null; // will hold reference to timer

function countDown(){
  if(x === -1){ return; } // exit function when timer reaches 0
  console.log(x);

  // recursive call to current function will establish a new timer
  // with whatever the delay variable currently is
  setTimeout(countDown, int); 

  int -= 10; // Decrease the delay for the next timer
  x--;       // Decrease the display data
}

countDown();

答案 2 :(得分:0)

正如temmu所说,您不能。但是,每次创建一个新的间隔都是一个坏主意,因为这会导致严重的内存泄漏。

改为使用setTimeout

var x = 0,
    int = 1000;

function start()
{
   setTimeout(function() {
      console.log(x++);
      int -= 50;
      start();
   }, int);
}

start();

答案 3 :(得分:0)

也可以通过重新启动该功能来使用setInterval

(function(){
  var x = 0;
  var interval = 1000; //1000ms interval
  var decreaseBy = 100;
  var scheduler;
  
  function setScheduler(){
    scheduler = null;
    scheduler = setInterval(function(){
      intervalTask();
    }, interval);
  }
  
  function intervalTask() {
    
    // function that you want to execute
    console.log(x += 1);

    console.log("current interval: " + interval);
    
    clearInterval(scheduler);
    
    if(interval <= 0) {
      return;
    }
    
    interval = interval - decreaseBy;
    
    // define it again to reinitiate the interval
    setScheduler();
  }
  
  // start it when you need it to
  setScheduler();
  
})();

答案 4 :(得分:0)

据我了解,您的想法是逐渐改变int的价值。我为您提出了一些小算法。我将int更改为step value, which is becoming roughly 2 times smaller every time we get to the middle of the previous top`的值。这是代码:

var x = 0,
    top = 1000,
    middle = top / 2,
    int = 1000,
    step = 50,
    minStep = 5;

function start()
{
    if (int <= middle && step > minStep) {
        top = middle;
        middle = top / 2;
        if (middle % 50) {
            middle = Math.floor(middle / 50) * 50;
        }
        step /= 2
        if (step % 5) {
            step = (Math.floor(step / 5) + 1) * 5;
        }
    }
    if (!int) return;
    setTimeout(function() {
       console.log(x++);
       // to check how int changes: console.log("int: " + int);
       int -= step;
       start();
    }, int);
}

start();

希望这很有用。