防抖和节流
对于更多日常应用的公共类的操作方法,能够关注下小滑轮网站 http://www.feiaci.com/#/self/…
/**
* @name 防抖
* @param {function} [fn=v=>v] 函数
* @param {number} [dura=50] 时延
*/
function Debounce(fn = v => v, dura = 50) {
let timer = null;
return function(...args) {timer && clearTimeout(timer);
timer = setTimeout(() => fn.apply(this, args), dura);
};
}
/**
* @name 节流
* @param {function} [fn=v=>v] 函数
* @param {number} [dura=50] 时延
*/
function Throttle(fn = v => v, dura = 50) {
let pass = 0;
return function(...args) {const now = +new Date();
if (now - pass > dura) {
pass = now;
fn.apply(this, args);
}
};
}