最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to execute functions with delay, underscorejslodash - Stack Overflow

programmeradmin1浏览0评论

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 the numbers so fast!

The solution? Throtlle/debounce/delay the prints using underscorejs or lodash.

Tentative

My first tentative was to use throttle.

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

let throttleFun = _.throttle(fun, 1000);

for (let i = 0; i < 10; i++) {
  throttleFun(i);
}
<script src=".js/1.8.3/underscore-min.js"></script>

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 the numbers so fast!

The solution? Throtlle/debounce/delay the prints using underscorejs or lodash.

Tentative

My first tentative was to use throttle.

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

let throttleFun = _.throttle(fun, 1000);

for (let i = 0; i < 10; i++) {
  throttleFun(i);
}
<script src="https://cdnjs.cloudflare./ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

But instead of printing all the numbers with a 1000ms of separation, it just calls the function every 1000ms, which causes it to only actually execute twice.

My next try, was to use debounce:

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

let debouncedFn = _.debounce(fun, 1000);

for (let i = 0; i < 10; i++) {
  debouncedFn(i);
}
<script src="https://cdnjs.cloudflare./ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

Now, again, instead of printing the values with 1000ms of separation, debounce actually delays the execution of my function since it last executed by 1000ms, which causes it to only execute once.

Question

All the behaviors I previously mentioned are documented, and they are the same for both underscore and lodash.

My question here is:

  • Using one of the previous libraries (or another similar one), is there a way to print the arguments the function receives in the console with 1000ms of separation between them, using a similar function style?

Please note that while in this case the arguments happen to be numbers, in reality they can be strings, objects or any other thing without any co-relation whatsoever.

Share Improve this question edited Mar 2, 2017 at 11:22 Flame_Phoenix asked Mar 2, 2017 at 10:10 Flame_PhoenixFlame_Phoenix 17.7k40 gold badges146 silver badges284 bronze badges 2
  • 1 did you look at setInterval ? – user1496463 Commented Mar 2, 2017 at 10:12
  • Yes, and I fail to see how it would be of any help. – Flame_Phoenix Commented Mar 2, 2017 at 11:23
Add a ment  | 

1 Answer 1

Reset to default 4

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);
}
发布评论

评论列表(0)

  1. 暂无评论