How can I alert an asynchronous function that another instance of the same function has fired?

▼魔方 西西 提交于 2020-03-04 18:58:30

问题


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;
   }
 }

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.

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?


回答1:


Earlier today I asked a question similar to this and someone by the username of @FedericoMoretti was able to help me out. He suggested using let and const variables which did the trick in my specific case.

 let bar = 0;

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



回答2:


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

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

Basically something like this:


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);
  };
})();  

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');


来源:https://stackoverflow.com/questions/60219494/how-can-i-alert-an-asynchronous-function-that-another-instance-of-the-same-funct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!