How to perform an action every couple of seconds?

后端 未结 6 1815
终归单人心
终归单人心 2020-12-20 14:25

Can someone quickly and simply explain to me how to perform an action every couple of seconds using

var timeOut = setTimeout(FunctionName, 5000);
         


        
6条回答
  •  眼角桃花
    2020-12-20 14:50

    As you asked for a method using setTimeout:

    function doStuff() {
       console.log("hello!");
       setTimeout(doStuff, 5000);
    }
    setTimeout(doStuff, 5000);
    

    But it would probably be better to use setInterval:

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

提交回复
热议问题