Ah, good old debounce gets an object-oriented makeover.
Once armed, the task will trigger with the given delay. However, if the main spring is armed again before time's over, the delay restarts.
Install with npm:
npm i mainspring-debounce
Then:
import { Mainspring } from 'mainspring-debounce';
First, create a mainspring object with a callback function and (optionally) a delay in MS.
new Mainspring({
callback: () => {...},
delay: 2000
});
Then, just call the arm() function to arm, and cancel() function to cancel.