跳到主要内容

简版防抖节流函数

节流函数

function throttle(func, delay) {
let lastTime = 0;
return function () {
const now = Date.now();
if (now - lastTime >= delay) {
lastTime = now;
func.apply(this, arguments);
}
};
}

const throttledFunction = throttle(
() => console.log('节流函数'),
1000
);

防抖函数

function debounce(func, delay) {
let timeoutId;
return function () {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
func.apply(this, arguments);
}, delay);
};
}

const debouncedFunction = debounce(
() => console.log('防抖函数'),
1000
);