-
1.什么是函数防抖[debounce]?
- 函数防抖是优化高频率执行js代码的一种手段
- 可以让被调用的函数在一次连续的高频操作中只被调用一次
-
2. 函数防抖的作用
- 减少代码执行次数, 提升网页性能
-
3. 函数防抖应用场景
- oninput / onmousemove / onscroll / onresize 等事件
let oInput = document.querySelector("input");
let timerId = null;
oInput.oninput = function () {
timerId && clearInterval(timerId);
timerId = setTimeout(function () {
console.log("发生网络请求");
}, 1000);
}
网友评论