0

I am writing a node function whose executions should be not too close in time (say 5 seconds).

I need to enforce the minimum delay inside the function, and not on the calling side, since its user should know nothing about the delay.
This is the reason why I can't use logic similar to delay or delayed node modules...

Note that I cannot simply setTimeout() wrap the function code either, since I want to enforce a minimum delay among executions (i.e.: the first execution should start immediately).

MarcoS
  • 17,323
  • 24
  • 96
  • 174
  • You can simply use setTimeout... – Jonas Wilms May 24 '17 at 17:40
  • 1
    What you're looking for is a throttle function. Check out https://stackoverflow.com/questions/27078285/simple-throttle-in-js for a function into which you can pass your function, along with a throttle time. – Arnav Aggarwal May 24 '17 at 17:43
  • @Jonas w: no, I can't: the function could be called 100 times at the same moment: after 5 seconds it should start 100 executions together... – MarcoS May 24 '17 at 17:56
  • @Arnav Aggarwal: I think I'll go with underscore throttle logic, thanks... – MarcoS May 24 '17 at 18:00
  • @MarcoS you could. Put all calls into an array, then iterate over them with setTimeout... – Jonas Wilms May 24 '17 at 18:17

1 Answers1

1

Have you looked at bottleneck? Maybe you want to wrap your function?

clonq
  • 319
  • 2
  • 11