仅在给定的时间内执行一次函数
我有一个API请求,该请求在给定的时间内被多次调用。更具体地说,此请求是为用户令牌刷新的,因此可以按每个请求进行调用,这很快就加在一起。我想创建一个函数,该函数告诉该函数不要在给定的秒数中运行。我曾尝试使用Lodash Debounce,但我无法正常工作。
let debounceRefresh;
debounceRefresh = debounce(() => {
api.request(){
});
}, 1000);
debounceRefresh();
我是在执行错误吗?有可能吗?
I have an api request that is called multiple times in a given amount of time. More specifically this request is for refreshing the user token, so it's called on every request, which adds up pretty quickly. I would like to create a function that tells the function not to run for a given amount of seconds. I have tried using lodash debounce but I can't get it to work.
let debounceRefresh;
debounceRefresh = debounce(() => {
api.request(){
});
}, 1000);
debounceRefresh();
Am I executing this wrong? Is it possible to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您肯定需要
油门
才能完成工作。snippet来自
Yes, you definitely need
throttle
for the job.Snippet from github
您是否尝试过超时?
如果再次调用该函数,则可以清除超时并将其重置
clearTimeOut(myTimeOut);
为什么不使用其他侦听器?也许收到数据时?
Have you tried with a timeout?
If the function is called again, you can clear the timeout and reset it
clearTimeout(myTimeout);
Why don't you use a different listener? Perhaps when data is received?