function debounce(fn, delay)
{
let timeout = null;
return function() {
clearTimeout(timeout);
timeout = setTimeout(() =>
{
fn.apply(this, arguments);
}, delay);
}}
上述为绑定事件的防抖函数
第一次触发时timeout被定时器回调了一个id值
当delay期间再次触发防抖函数时 timeout被重新声明为null 为什么在回调函数中的ClearTimeout依然能够根据timeout找到需要被清空的定时器 还是说timeout=null相当于执行清空 那为什么还要在回调函数中再次清空呢 求指点
{
let timeout = null;
return function() {
clearTimeout(timeout);
timeout = setTimeout(() =>
{
fn.apply(this, arguments);
}, delay);
}}
上述为绑定事件的防抖函数
第一次触发时timeout被定时器回调了一个id值
当delay期间再次触发防抖函数时 timeout被重新声明为null 为什么在回调函数中的ClearTimeout依然能够根据timeout找到需要被清空的定时器 还是说timeout=null相当于执行清空 那为什么还要在回调函数中再次清空呢 求指点