/**
* 防抖函数
* @param {fun,duration} 执行函数,时间
* @return {fun} 返回一个回调函数;
*/
function debounce(fun, duration) {
let timer = null;
return function () {
// 清除定时器
if (timer) clearTimeout(timer);
let args = Array.prototype.slice.call(arguments);
timer = setTimeout(() => {
fun.apply(this, args);
}, duration);
};
}
let a = "a";
let b = "b";
let c = "c";
let d = "d";
// 传入多个参数
let myFunction = debounce((...reslute) => {
console.log(reslute);
}, 1000);
myFunction(a, b, c, d);
js防抖函数
最新推荐文章于 2024-06-16 18:09:06 发布