// 防抖函数 function debounce(func, delay, immediate = false) { let timer = null; return function () { const context = this; const args = arguments; const callNow = immediate && !timer; // 如果定时器已经存在,清除它 if (timer) { clearTimeout(timer); } // 设置一个新的定时器 timer = setTimeout(() => { if (!immediate) { func.apply(context, args); } timer = null; }, delay); // 配置立即执行 if(callNow) { func.apply(context, args) } }; }