一.什么是函数防抖
函数防抖(debounce),就是指触发事件后,在 n 秒内函数只能执行一次,如果触发事件后在 n 秒内又触发了事件,则会重新计算函数延执行时间(在这里和函数节流区分一下,函数节流是在触发完事件之后的一段时间之内不能再次触发事件)。
二.函数防抖的实现思路
在触发事件的时候,设置一个定时器,如果之前就已经触发过该事件了,就清除之前设置的定时器,重新设置一个定时器,如果在触发事件之后没有再次触发事件,那么就等待计时结束执行函数。
三.代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| function debounce(fn, delay) { if(typeof fn!=='function') { throw new TypeError('fn不是函数') } let timer; return function () { var _this = this; var args = arguments; if (timer) { clearTimeout(timer); } timer = setTimeout(function () { fn.apply(_this, args); }, delay); }; }
|
四.函数防抖的使用场景
1.搜索框搜索输入。只需用户最后一次输入完,再发送请求;
2.用户名、手机号、邮箱输入验证;
3.浏览器窗口大小改变后,只需窗口调整完后,再执行 resize 事件中的代码,防止重复渲染。
五.防抖的立即执行与非立即执行
函数防抖其实是分为 “立即执行版” 和 “非立即执行版” 的,根据字面意思就可以发现他们的差别,所谓立即执行版就是 触发事件后函数不会立即执行,而是在 n 秒后执行,如果在 n 秒内又触发了事件,则会重新计算函数执行时间。 而 “非立即执行版” 指的是 触发事件后函数会立即执行,然后 n 秒内不触发事件才能继续执行函数的效果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
function debounce(func,wait,immediate) { let timer; return function () { let context = this; let args = arguments;
if (timer) clearTimeout(timer); if (immediate) { var callNow = !timer; timer = setTimeout(() => { timer = null; }, wait) if (callNow) func.apply(context, args) } else { timer = setTimeout(function(){ func.apply(context, args) }, wait); } } }
|