Skip to content Skip to sidebar Skip to footer

How To Make Setinterval Wait Until The Function Execute?

i have a setInterval that execute a function every 100 ms the problem is the setInterval does not wait until the function completely execute here is the code : function Main(){

Solution 1:

const anyFunction = () => {
  return new Promise((resolve, reject) => setTimeout(resolve, 2000));
}

const cycleAsync = (fn, time) => {
  const timerId = setTimeout(() => {
    fn()
      .then(() => {
        clearTimeout(timerId);
        cycle(fn, time);
      });
  }, time);
}


cycleAsync(anyFunction, 200);

Post a Comment for "How To Make Setinterval Wait Until The Function Execute?"