export default (func, threshold = 500, immediate = false) => {
if (typeof func !== 'function') {
throw new Error('First argument of debounce function should be a function');
}
let timer = null;
return function debounced(...args) {
const context = this;
const callNow = immediate && !timer;
const later = () => {
timer = null;
if (!immediate) func.apply(context, args);
};
clearTimeout(timer);
timer = setTimeout(later, threshold);
if (callNow) func.apply(context, args);
// console.log(func);
};
};
简单节流防抖函数
最新推荐文章于 2023-06-25 19:36:38 发布