//防抖
function debounce(fn, delay) {
let timerId = null;
return function () {
let self = this;
let args = arguments;
timerId && clearTimeout(timerId);
timerId = setTimeout(function () {
fn.apply(self, args);
}, delay || 1000);
}
}
//节流
function throttle(fn, delay) { // fn = test
let timerId = null;
let flag = true;
return function () {
if(!flag) return;
flag = false;
let self = this;
let args = arguments;
timerId && clearTimeout(timerId);
timerId = setTimeout(function () {
flag = true;
fn.apply(self, args);
}, delay || 1000);
}
}