/** * 防抖函数 * 实现原理: 每次触发事件时都取消之前的延时调用方法 * 应用场景: 按钮提交、搜索框输入、窗口大小改变事件 * 实现方式: 利用setTimeout返回的id清除延时调用 * * @example * const inputFunc = debounce((e) => { * console.log('input'); * }, 1000); * input.addEventListener('input', inputFunc); * */ /** * 防抖函数 * @param func 要执行的函数 * @param wait 等待时间 * @param immediate 是否立即执行 (true: leading模式,false: trailing模式) * @returns 防抖函数 */ declare function debounce any>(func: T, wait: number, immediate?: boolean): T & { cancel: () => void; }; /** * 防抖函数 * // 1. 非立即执行版 (trailing模式, 滞后执行) function debounce(func, wait) { let timer = null; return (...args) => { clearTimeout(timer); timer = setTimeout(() => { func.apply(this, args); }, wait); } } // 2. 支持立即执行版 function debounce(func, wait, immediate) { let timer = null; return function(...args) { clearTimeout(timer); timer = setTimeout(() => { timer = null; // 2.滞后执行, trailing模式 if (!immediate) func.apply(this, args); }, wait); if (immediate && !timer) { // 1.立即执行, leading模式 func.apply(this, args); } } } */ export { debounce as default };