在学习 JS 中的 Debounce 时,我看到了 2 种不同的方法。在函数声明示例中调用回调与在箭头函数示例中使用 apply 绑定调用之间有区别吗?
function debounce(cb, delay) {
let timeoutId;
return function(...args) {
clearTimeout(timeoutId)
timeoutId = setTimeout(() => {
cb(args)
}, delay);
}
}
VS
function debounce(func, delay){
let timeoutId;
return (...args) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
func.apply(this, args);
}, delay);
};
}