Angular 4 setTimeout() with variable delay and wait

You can use IIFE (Immediately Invoked Function Expression) and function recursion instead. Like this:

let i = 0;
(function repeat(){
  if (++i > 5) return;
  setTimeout(function(){
    console.log("Iteration: " + i);
    repeat();
  }, 5000);
})();

Live fiddle here.

Leave a Comment