Execute the setInterval function without delay the first time

后端 未结 14 2151
故里飘歌
故里飘歌 2020-11-22 16:17

It\'s there a way to configure the setInterval method of javascript to execute the method immediately and then executes with the timer

相关标签:
14条回答
  • 2020-11-22 16:26

    I'm not sure if I'm understanding you correctly, but you could easily do something like this:

    setInterval(function hello() {
      console.log('world');
      return hello;
    }(), 5000);
    

    There's obviously any number of ways of doing this, but that's the most concise way I can think of.

    0 讨论(0)
  • 2020-11-22 16:29

    Here's a simple version for novices without all the messing around. It just declares the function, calls it, then starts the interval. That's it.

    //Declare your function here
    function My_Function(){
      console.log("foo");
    }    
    
    //Call the function first
    My_Function();
    
    //Set the interval
    var interval = window.setInterval( My_Function, 500 );

    0 讨论(0)
  • 2020-11-22 16:31

    To solve this problem , I run the function a first time after the page has loaded.

    function foo(){ ... }
    
    window.onload = function() {
       foo();
    };
    
    window.setInterval(function()
    {
        foo(); 
    }, 5000);
    
    0 讨论(0)
  • 2020-11-22 16:32

    For someone needs to bring the outer this inside as if it's an arrow function.

    (function f() {
        this.emit("...");
        setTimeout(f.bind(this), 1000);
    }).bind(this)();
    

    If the above producing garbage bothers you, you can make a closure instead.

    (that => {
        (function f() {
            that.emit("...");
            setTimeout(f, 1000);
        })();
    })(this);
    

    Or maybe consider using the @autobind decorator depending on your code.

    0 讨论(0)
  • 2020-11-22 16:35

    Here's a wrapper to pretty-fy it if you need it:

    (function() {
        var originalSetInterval = window.setInterval;
    
        window.setInterval = function(fn, delay, runImmediately) {
            if(runImmediately) fn();
            return originalSetInterval(fn, delay);
        };
    })();
    

    Set the third argument of setInterval to true and it'll run for the first time immediately after calling setInterval:

    setInterval(function() { console.log("hello world"); }, 5000, true);
    

    Or omit the third argument and it will retain its original behaviour:

    setInterval(function() { console.log("hello world"); }, 5000);
    

    Some browsers support additional arguments for setInterval which this wrapper doesn't take into account; I think these are rarely used, but keep that in mind if you do need them.

    0 讨论(0)
  • 2020-11-22 16:35

    actually the quickest is to do

    interval = setInterval(myFunction(),45000)
    

    this will call myfunction, and then will do it agaian every 45 seconds which is different than doing

    interval = setInterval(myfunction, 45000)
    

    which won't call it, but schedule it only

    0 讨论(0)
提交回复
热议问题