I'm using React,
I have a button that calls an asynchronous function on every onClick event:
<MyButton onClick={handleClick}>
Next
</MyButton>
Then a function that takes time to load data:
const isExecuting = useRef(false);
const handleClick = async () => {
if (!isExecuting.current) {
isExecuting.current = true;
try {
resetViews();
await chargeViews(patientId);
} finally {
isExecuting.current = false;
}
}
};
So for example when I click 5 times, it will receive 5 calls and all of them will be executed in order, I need a way to just execute the latest call and ignore the previous 4 calls, so it won't take time to execute all of them.
PS: I thought of disabling the button until the function finishes executing, but as I'm using the button to load the next patient
, this won't be convinient because we'd be obliged to wait for 4 patients to be loaded to load the 5th patient.