如何警告异步函数该函数的另一个实例已触发? [英] How can I alert an asynchronous function that another instance of the same function has fired?

查看:82
本文介绍了如何警告异步函数该函数的另一个实例已触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的函数内创建一个警报,以跟踪是否在15秒内触发了同一函数的任何其他实例.这是我到目前为止的内容:

I am wanting to create an alert inside my function which tracks if any other instances of the same function have fired during a 15 second period. this is what I have so far:

 bar = 0;

 async function Counting() {
   bar += 1;
   foo = bar;
   new Promise(resolve => setTimeout(resolve, 5000));
   if (bar == foo) {
     //Do something
   }
   else {
     return;
   }
 }

我使用bar作为全局计数器,使用foo作为函数实例计数器,但是由于某种原因,该函数的所有实例都同时更新.

I'm using bar as a global counter and foo as a function instance counter, but for some reason all instances of the function update at the same time.

在这种情况下,如何检查此功能是否已在15秒的等待时间内触发,然后停止该功能的所有以前的实例?

How can I check if this function has fired during the 15 second waiting period and then stop all previous instances of the function if this is the case?

推荐答案

我觉得您的解决方案有点过于复杂.我认为您所描述的是功能反跳:

I feel like your solution is a bit overly complicated. I think what you're describing is function debouncing:

可能很难解释.进行反跳操作时,就像嘿,除非知道入站没有更多更改,否则我将不执行该功能".直到其他所有人都满意并且我们明确要继续进行之后,我们才会执行我们的功能.想象一下在餐厅点菜.您开始向服务员列出项目,最后他们问这是否一切?"如果是的话,他们会把您留给您,然后去拿食物和饮料.如果不是,则将其添加到订单中,然后他们再次询问您,直到可以继续进行. -使用JavaScript进行节流和反跳-Jhey Tompkins - https://codeburst.io/throttling-and-debouncing-in-javascript -b01cad5c8edf

It can be a little harder to explain. With debouncing, it’s like "Hey, I’m not going to execute that function until I know there are no more changes inbound". We don’t execute our function until everyone else is happy and we’re clear to proceed. Imagine ordering food at a restaurant. You start listing off items to the waiter/waitress and at the end they ask "Is that everything?" If it is, they leave you to it and go get your food and drinks. If it isn’t, you add to the order and then they ask you again until they are clear to proceed. - Throttling and Debouncing in JavaScript - Jhey Tompkins - https://codeburst.io/throttling-and-debouncing-in-javascript-b01cad5c8edf

基本上是这样的:


const fn = (() => {
  let timer = null;
  const myFn = () => {
    timer = null;
    console.log('My fn has been ran!');
  };
  return () => {
    if(timer != null){
      clearTimeout(timer);
    }
    timer = setTimeout(myFn,5000);
  };
})();  

这样,当您调用函数时,它将等待15秒才能运行实际的逻辑.但是,如果在15秒钟内再次调用该函数,计时器将重新启动.

This way when you call your function, it will wait 15 seconds to run the actual logic. However, if the function is called again within that 15 seconds, the timer restarts.

通常对于类似这样的事情,您可以使用库函数来完成,因此您不必每次都写所有这些样板:

Usually for something like this you use a library function to do it so you don't have to write all this boilerplate each time:

function debounce(fn,time){
  let timer = null;
  return function(...args){
    if(timer != null){
      clearTimeout(timer);
    }
    timer = setTimeout(fn.bind(this,...args),5000);
  };
}

// Usage
const myFn = debounce((name) => {
  console.log('Hello %s!',name);
},1000);

// This function won't output "Hello !"
myFn('Joe');
// This function will
myFn('John');

这篇关于如何警告异步函数该函数的另一个实例已触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆