防抖和节流
手写一个防抖/节流 函数
防抖
js
// 防抖
function debounce(fn, wait) {
let timer = null;
return function () {
let _this = this;
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(function () {
fn.apply(_this, arguments);
timer = null;
}, wait);
};
}
document.addEventListener('mousemove', function () {
fn();
});
节流
js
// 节流
function throttle(fn, wait) {
let timer = 0; // 初始值为0 则:首次立即执行 Infinity 则首次不立即执行
return function () {
let _this = this;
if (Date.now() - timer >= wait) {
fn.apply(_this, arguments);
}
timer = Date.now();
};
}
const fn = throttle(function () {
console.log(111);
}, 1000);
document.addEventListener('mousemove', function () {
fn();
});