function debounce(fn, delay) {
let timer = null;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => {
console.log(this, 'debounce.this')
fn.apply(this, args)
}, delay);
};
};
function fn() {
console.log(this, 'fn.this')
}
let testObjdebounce2 = new Object()
testObjdebounce2.debounce = debounce
let testObjFn2 = new Object()
testObjFn2.fn = fn
testObjdebounce2.debounce(fn, 1000)()
function throttle(fn, delay = 500) {
let flag = true
return (...args) => {
if (!flag) return
setTimout(() => {
fn.apply(this, args)
flag = true
}, delay)
}
}