The debounce
utility is a simple JavaScript function that limits the rate at which a particular callback function can be executed. It ensures that a function is only called after a specified delay period has passed since the last time it was invoked. This is particularly useful for optimizing performance in scenarios like handling user input events, where rapid calls can lead to unnecessary processing.
npm install @webfactoryde/debounce
Import the debounce
function in your module(s) and pass a callback that you want to rate-limit:
// your module
import debounce from '@webfactoryde/debounce';
function coolFunction() {
// do cool stuff
}
window.addEventListener('resize', debounce(coolFunction, 200));