Throttle JavaScript function calls, but with queuing (don't discard calls)

后端 未结 3 1869
醉梦人生
醉梦人生 2021-02-08 00:02

How can a function rate-limit its calls? The calls should not be discarded if too frequent, but rather be queued up and spaced out in time, X milliseconds apart. I\'ve looked at

3条回答
  •  再見小時候
    2021-02-08 00:32

    While the snippets offered by others do work (I've built a library based on them), for those who want to use well-supported modules, here are the top choices:

    • the most popular rate limiter is limiter
    • function-rate-limit has a simple API that works, and good usage stats on npmjs
    • valvelet, a newer module, claims to do an even better job by supporting promises, but hasn't gained popularity yet

提交回复
热议问题