且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Underscore.js/lodash如何延迟执行函数

更新时间:2023-09-29 11:17:04

您可以使用本机setInterval函数来实现:

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);
}