var debounce = function(fn,delay){
var timeoutId;
return function debounced(){
if(timeoutId){
clearTimeout(timeoutId);
}
timeoutId = setTimeout(fn.bind(this),delay,arguments);
}
}
Run Code Online (Sandbox Code Playgroud)
上面的函数是一种简单的去抖动方法吗?我想知道它是否正确实施。有什么缺陷吗?
有什么缺陷吗?
是的。该setTimeout函数不采用arguments数组作为第三个参数。它可以采用两个以上的参数,但这些参数被鄙视,因为它们不向后兼容遗留引擎。请继续阅读setTimeoutMDN。所以最好一起去
var that = this,
args = arguments;
timeoutId = setTimeout(function() {
fn.apply(that, args);
}, delay);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2994 次 |
| 最近记录: |