【发布时间】:2019-12-18 16:27:40
【问题描述】:
我刚刚看到这个关于去抖动的教程,但是当他使用debounceSayHello("Jeremy") 时,我对如何在去抖动中通过所有这些函数传递名称参数感到非常困惑。为什么debounce(sayHello,3000) 而不是debounce(()=>sayHello(name),3000)?而当定义内部返回的匿名函数时,那里没有参数,'Jeremy'是如何传入并最终到达apply函数的?非常感谢!
function debounce (func, delay) {
let timerId;
return function () {
if (timerId) {
clearTimeout(timerId)
}
timerId=setTimeout(()=>func.apply(this,[...arguments]),delay);
}
}
function sayHello(name){
console.log(`Hello ${name}`);
}
let debouncedSayHello=debounce(sayHello,3000);
debouncedSayHello('Jeremy')
//Hello Jeremy
原文:
【问题讨论】:
-
sayHello(name)会立即调用该函数并执行它并发送该方法返回的内容。
标签: javascript this apply bind debounce