Skip to content Skip to sidebar Skip to footer

How To Execute Functions With Delay, Underscorejs/lodash

Background I have a JavaScript function that prints numbers to the screen. This function prints a lot of numbers, but I am using a screen as old as Alan Turing so it can't print th

Solution 1:

You can do it with the native setInterval function:

let fun = num => {
  console.log(num);
};

let max = 10, i = 0;

let timer = setInterval(() => {
  fun(i);
  i++;
  if (i === max) {
    clearInterval(timer);
  }
}, 1000);

If you want to use a underscore/lodash, you do do something like so:

let fun = num => {
  console.log(num);
};

for (let i = 0; i < 10; i++) {
    _.delay(fun, 1000 * (i + 1), i);
}

Post a Comment for "How To Execute Functions With Delay, Underscorejs/lodash"