## Notes
- Makes sure code is only triggered once--cancels any in-flight
Used when user changes input and requires fetching another request from an API.
```javascript
function debounce(func, timeout = 300) {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => {
func.apply(this, args);
}, timeout);
};
}
function saveInput() {
console.log('Saving data");
}
const processChange = debounce(() => saveInput());
```
Traditional debounce cancels everything before the last invocation. Inverse debounce will cancel everything after the first invocation
## Inverse Debounce
```javascript
function debounceInverse(func, timeout = 300) {
let timer;
return (...args) {
if (!timer) {
func.apply(this, args);
}
clearTimeout(timer);
timer = setTimeout(() => {
timer = undefined;
}, timeout);
};
}
```
## References
- [JavaScript Debounce Example - FreeCodeCamp](https://www.freecodecamp.org/news/javascript-debounce-example/)
- [[JavaScript Study MOC]]