防抖与节流
...小于 1 分钟
防抖与节流
复习一下
function debounce(fn, delay) {
let timerID = null;
return function () {
const context = this;
if (timeID) {
window.clearTimeout(timerID);
}
timerID = setTimeout(() => {
fn.apply(context, arguments);
}, delay);
};
}
function thottle(fn) {
/* 节流 */
let cardu = true;
if (cardu) {
fn.apply(this, arguments);
cardu = false;
setTimeout(() => {
cardu = true;
}, 3000);
}
}
赞助