Javascript setTimeout function repeat

前端 未结 3 1617
走了就别回头了
走了就别回头了 2020-12-21 12:26

Is there a way to repeat a function in Javascript using the setTimeout function? For example, I need two functions to be called every five seconds. I have something like thi

相关标签:
3条回答
  • 2020-12-21 13:06

    Every x seconds can be done with setInterval:

    $(document).ready(function(){    
      setInterval(function(){
        shiftLeft(); showProducts();
      }, 5000);    
    });
    
    0 讨论(0)
  • 2020-12-21 13:25
    $(document).ready(function(){    
      setTimeout(function(){
        shiftLeft(); showProducts();
      }, 5000);    
    });
    
    0 讨论(0)
  • 2020-12-21 13:28

    Use setInterval() instead of setTimeout(), if you want your function to be executed repeatedly. setTimeout() delays the execution of your function for x seconds, while setInterval() executes your function every x seconds.

    Both within the boundaries of the event queue of JavaScript, so don't be too confident, that you functions get executed at the exact time you specified

    $(document).ready(function(){    
        setInterval( function(){ shiftLeft(); showProducts(); }, 5000);    
    }); 
    
    0 讨论(0)
提交回复
热议问题