是什么
本质上是优化高频率执行代码的一种手段
如:浏览器的 resize
、scroll
、keypress
、mousemove
等事件在触发时,会不断地调用绑定在事件上的回调函数,极大地浪费资源,降低前端性能.
为了优化体验,需要对这类事件进行调用次数的限制,对此我们就可以采用 throttle
(节流)和debounce
(防抖)的方式来减少调用频率
定义
- 节流: n 秒内只运行一次,若在 n 秒内重复触发,只有一次生效
- 防抖: n 秒后在执行该事件,若在 n 秒内被重复触发,则重新计时
代码实现
防抖
<script>
function show() {
console.log('666');
}
//防抖函数
function debounce(func) {
func();
}
const btn = document.querySelector('button');
btn.addEventListener('click', debounce(show));
</script>
可以发现防抖函数中直接执行show函数时,还没有点击按钮时,控制台就出现了点击按钮的消息,这样写的话,在定义监听函数的时候就直接执行了函数.
可以采用高阶函数(在函数中返回函数)的方式解决
<script>
function show() {
console.log('666');
}
//防抖函数
function debounce(func) {
//高阶函数
return function () {
func();
};
}
const btn = document.querySelector('button');
btn.addEventListener('click', debounce(show));
</script>
//防抖函数
function debounce(func, delay) {
//高阶函数
return function () {
let timer;
clearTimeout(timer);
timer = setTimeout(function () {
func();
}, delay);
};
}
清除延时要在建立延时的前面,连续点击按钮5次,全部消息都陆续执行,并没有实现重新计时的功能,是因为这些独立的执行函数之间并没有任何关系.
可以采用闭包的方式,将timer这个变量定义放在函数的外围,因为作用域链的关系,所有独立函数都能访问到timer这个变量
//防抖函数
function debounce(func, delay) {
let timer;
//高阶函数
return function () {
clearTimeout(timer);
timer = setTimeout(function () {
func();
}, delay);
};
}
点击按钮5次,控制台也只显示一条信息
防抖功能简单实现:
<script>
function show() {
console.log('666');
}
//防抖函数
function debounce(func, delay) {
let timer;
//高阶函数
return function () {
let context = this; // 保存this指向
let args = arguments; // 拿到event对象
clearTimeout(timer);
timer = setTimeout(function () {
func.apply(context, args);
}, delay);
};
}
const btn = document.querySelector('button');
btn.addEventListener('click', debounce(show, 1000));
</script>
防抖如果需要立即执行,可加入第三个参数用于判断,实现如下
function show() {
console.log('666');
}
//防抖函数
function debounce(func, delay, immediate) {
let timer;
//高阶函数
return function () {
let context = this; // 保存this指向
let args = arguments; // 拿到event对象
if (timer) clearTimeout(timer); // timer 不为null
if (immediate) {
// 第一次会立即执行,以后只有事件执行后才会再次触发
let callNow = !timer;
timer = setTimeout(function () {
timer = null;
}, delay);
if (callNow) {
func.apply(context, args);
}
} else {
timer = setTimeout(function () {
func.apply(context, args);
}, delay);
}
};
}
const btn = document.querySelector('button');
btn.addEventListener('click', debounce(show, 1000, 1));
节流
完成节流可以使用时间戳与定时器的写法
使用定时器写法,delay
毫秒后第一次执行,第二次事件停止触发后依然会再一次执行
function coloring() {
const r = Math.floor(Math.random() * 255);
const g = Math.floor(Math.random() * 255);
const b = Math.floor(Math.random() * 255);
document.body.style.backgroundColor = `rgb(${r},${g},${b})`;
}
function throttle(func, delay) {
let timer;
return function () {
let context = this;
let args = arguments;
//判断触发事件是否在时间间隔内
/* timer被赋值了,表示任务还在等待执行,暂时不改变timer的值,
如果timer没被赋值,就给它赋值执行任务 */
if (timer) {
return;
}
timer = setTimeout(function () {
func.apply(context, args);
timer = null;
}, delay);
};
}
window.addEventListener('resize', throttle(coloring, 1000));
使用时间戳写法,事件会立即执行,停止触发后没有办法再次执行
function coloring() {
const r = Math.floor(Math.random() * 255);
const g = Math.floor(Math.random() * 255);
const b = Math.floor(Math.random() * 255);
document.body.style.backgroundColor = `rgb(${r},${g},${b})`;
}
function throttle(func, delay) {
let pre = 0;
return function () {
let now = new Date();
let context = this;
let args = arguments;
if (now - pre > delay) {
func.apply(context, args);
pre = now;
}
};
}
window.addEventListener('resize', throttle(coloring, 1000));
区别
相同点:
- 都可以通过使用
setTimeout
实现- 目的都是,降低回调执行频率。节省计算资源
不同点:
- 函数防抖,在一段连续操作结束后,处理回调,利用
clearTimeout
和setTimeout
实现。函数节流,在一段连续操作中,每一段时间只执行一次,频率较高的事件中使用来提高性能- 函数防抖关注一定时间连续触发的事件,只在最后执行一次,而函数节流一段时间内只执行一次
例如,都设置时间频率为500ms,在2秒时间内,频繁触发函数,节流,每隔 500ms 就执行一次。防抖,则不管调动多少次方法,在2s后,只会执行一次
如下图所示:
应用场景
防抖在连续的事件,只需触发一次回调的场景有:
- 搜索框搜索输入。只需用户最后一次输入完,再发送请求
- 手机号、邮箱验证输入检测
- 窗口大小
resize
。只需窗口调整完成后,计算窗口大小。防止重复渲染。
节流在间隔一段时间执行一次回调的场景有:
- 滚动加载,加载更多或滚到底部监听
- 搜索框,搜索联想功能