Execute the setInterval function without delay the first time

后端 未结 14 2332
故里飘歌
故里飘歌 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:41

    I will suggest calling the functions in the following sequence

    var _timer = setInterval(foo, delay, params);
    foo(params)
    

    You can also pass the _timer to the foo, if you want to clearInterval(_timer) on a certain condition

    var _timer = setInterval(function() { foo(_timer, params) }, delay);
    foo(_timer, params);
    

提交回复
热议问题