Ad

How To Have A Block Of Code Run Then Loop It Every Few Minutes

- 1 answer

I want to have a block of code run right away and then loop it every few minutes. I was using setInterval but it waits the amount of time you give it and then runs the block of code and loops.

Ad

Answer

So you have to define that code inside function then call that function first then add set interval code and pass this function to it

  function s () {}
 s();
setInterval(s, 100)

Or you can use set timeOut inside this function and run it recursively, that's will be good in asynchronous calls you can use await to wait for response then run this function again after timeout, because setInterval will cause overlap in that case.

  async function s() {
       let a = await calldb;
      setTimeout (s,1000)
  }

  s(); 
Ad
source: stackoverflow.com
Ad