防抖函数
防抖
防抖就是在规定的时间内只能触发一次函数。
简易版防抖
将多次执行变成最后一次执行。1
2
3
4
5
6
7
8
9
10
11
12
13function debounce(fn, wait = 50) {
let timeout;
return function (...args) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
timeout = setTimeout(() => {
fn.apply(this, args);
});
}
}可以立即执行的防抖
1 | function debounce(fn, delay = 50, immediate = true) { |