20 lines
480 B
JavaScript
20 lines
480 B
JavaScript
// oxlint-disable-next-line no-explicit-any
|
|
export function debounce(fn, wait = 500) {
|
|
let timeout = null;
|
|
const debounced = (...args) => {
|
|
if (timeout !== null) {
|
|
clearTimeout(timeout);
|
|
}
|
|
timeout = setTimeout(() => {
|
|
fn(...args);
|
|
}, wait);
|
|
};
|
|
debounced.destroy = () => {
|
|
if (timeout !== null) {
|
|
clearTimeout(timeout);
|
|
timeout = null;
|
|
}
|
|
};
|
|
return debounced;
|
|
}
|