javascriptfunction debounce(fn, delay) {
let timer = null;
return function (...args) {
clearTimeout(timer);
timer = setTimeout(() => fn.apply(this, args), delay);
};
}jsconst onSearch = debounce((e) => fetchResults(e.target.value), 300);
input.addEventListener('input', onSearch);小贴士:搜索联想、窗口 resize、按钮防连点,都是防抖的经典场景。

扫码关注「前端达人」,干货代码每周更新