export function debounce<Args extends unknown[]>(
fn: (...args: Args) => void,
delay: number,
): (...args: Args) => void {
let timer: ReturnType<typeof setTimeout> | null = null;
return (...args) => {
if (timer) clearTimeout(timer);
timer = setTimeout(() => fn(...args), delay);
};
}
const onSearch = debounce((q: string) => {
fetch(`/search?q=${encodeURIComponent(q)}`);
}, 300);
input.addEventListener('input', e => onSearch((e.target as HTMLInputElement).value));
Create a free account and build your private vault. Share publicly whenever you want.