首页 文章

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

提问于
浏览
0
var x = 0;

var int = 1000; //1000ms interval

function Start(){
    setInterval(function(){
        console.log(x += 1);  //increasing x value by 1 every int miliseconds

        console.log("Interval: " + int);  //outputting interval value (in my case, it is always 1000ms, i want it to be for example: 1000ms, 950ms, 900ms, 850ms, ... , 15ms, 10ms, 5ms, 0ms)

    }, int); //how to increase/decrease this 'int' value gradually - (for example: 1000ms, 950ms, 900ms, 850ms, ... , 15ms, 10ms, 5ms, 0ms)
}

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

5 回答

  • 0

    一旦 Build 了计时器's time, it can' t就会被更改 . 解决方案是使用 setTimeout 而不是 setInterval ,以便您可以使用新延迟 Build 新计时器 . 然后每个连续的计时器可以使用不同的时间:

    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();
    
  • 0

    据我所知,你的想法是逐渐改变 Value . 我想出了一些算法 . 我通过 step value, which is becoming roughly 2 times smaller every time we get to the middle of the previous top值更改int` . 这是代码:

    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();
    

    希望这很有用 .

  • 0

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

    改为使用 setTimeout

    var x = 0,
        int = 1000;
    
    function start()
    {
       setTimeout(function() {
          console.log(x++);
          int -= 50;
          start();
       }, int);
    }
    
    start();
    
  • 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();
    
  • 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();
      
    })();
    

相关问题