仅在给定的时间内执行一次函数

发布于 2025-02-11 05:01:56 字数 295 浏览 1 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空名 2025-02-18 05:01:56

是的,您肯定需要油门才能完成工作。

// in this example we invoke a fn for a period of 10 sec, invoking it 2 times a second, but we can perceive that the original function is only invoked at most once per 2 seconds according to the parameter below:

var TOTAL_TIME_TO_RUN = 10000; // 10 sec
var THROTTLE_INTERVAL = 2000; // <= adjust this number to see throttling in action
var INVOCATION_INTERVAL = 500; // 0.5 sec

// regular fn
var punchClock = function punchClock() {
  console.log(new Date().toISOString() + ' - call api');
};

// wrap it and supply interval representing minimum delay between invocations
var throttledPunchClock = _.throttle(punchClock, THROTTLE_INTERVAL);

// set up looping
var intervalId = setInterval(function() {
  console.log("attempting call api");
  throttledPunchClock()
}, INVOCATION_INTERVAL);

// run the demo
setTimeout(() => clearInterval(intervalId), 10000)
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
<pre>
  var TOTAL_TIME_TO_RUN = 10000; // 10 sec
  var THROTTLE_INTERVAL = 2000; // < = adjust this number to see throttling in action
  var INVOCATION_INTERVAL = 500; // 0.5 sec
</pre>

snippet来自

Yes, you definitely need throttle for the job.

// in this example we invoke a fn for a period of 10 sec, invoking it 2 times a second, but we can perceive that the original function is only invoked at most once per 2 seconds according to the parameter below:

var TOTAL_TIME_TO_RUN = 10000; // 10 sec
var THROTTLE_INTERVAL = 2000; // <= adjust this number to see throttling in action
var INVOCATION_INTERVAL = 500; // 0.5 sec

// regular fn
var punchClock = function punchClock() {
  console.log(new Date().toISOString() + ' - call api');
};

// wrap it and supply interval representing minimum delay between invocations
var throttledPunchClock = _.throttle(punchClock, THROTTLE_INTERVAL);

// set up looping
var intervalId = setInterval(function() {
  console.log("attempting call api");
  throttledPunchClock()
}, INVOCATION_INTERVAL);

// run the demo
setTimeout(() => clearInterval(intervalId), 10000)
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
<pre>
  var TOTAL_TIME_TO_RUN = 10000; // 10 sec
  var THROTTLE_INTERVAL = 2000; // < = adjust this number to see throttling in action
  var INVOCATION_INTERVAL = 500; // 0.5 sec
</pre>

Snippet from github

只是一片海 2025-02-18 05:01:56

您是否尝试过超时?

const myTimeout = setTimeout(debounceRefresh, 1000);

如果再次调用该函数,则可以清除超时并将其重置

clearTimeOut(myTimeOut);

为什么不使用其他侦听器?也许收到数据时?

Have you tried with a timeout?

const myTimeout = setTimeout(debounceRefresh, 1000);

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文