/**
* 防抖
* @param {fn:"防抖 ", delay:"定时器时间"}
*
*/
function debounce(fn, delay) {
let timer = null;
if (typeof fn !== "function") {
console.log("fn涓嶆槸涓�沅釜鍑芥晞1�7");
}
return function () {
let content = this;
let args = arguments;
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
fn.apply(content, args);
}, delay || 1000);
};
}
/**
* 节流
* @param {fn:"节流", delay:"定时器时间"}
*
*/
function throttle(fn, delay) {
let open = true;
return function () {
let context = this,
args = arguments;
if (!open) {
return false;
}
open = false;
setTimeout(() => {
fn.apply(context, args);
open = true;
}, delay || 1000);
};
}
防抖节流方法封装
最新推荐文章于 2024-06-09 20:17:27 发布