mirror of
https://github.com/stackblitz/bolt.new
synced 2024-11-27 22:42:21 +00:00
18 lines
356 B
TypeScript
18 lines
356 B
TypeScript
export function debounce<Args extends any[]>(fn: (...args: Args) => void, delay = 100) {
|
|
if (delay === 0) {
|
|
return fn;
|
|
}
|
|
|
|
let timer: number | undefined;
|
|
|
|
return function <U>(this: U, ...args: Args) {
|
|
const context = this;
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = window.setTimeout(() => {
|
|
fn.apply(context, args);
|
|
}, delay);
|
|
};
|
|
}
|