function debounce(fn, delay){
var timer = null;
var handleFn = function(){
if (timer) clearTimeout(timer);
// 获取this和argument
var _this = this;
var _arguments = arguments;
timer = setTimeout(function(){
// 在执行时,通过apply来使用_this和_arguments
fn.apply(_this, _arguments);
}, delay);
}
// 取消处理
handleFn.cancel = function(){
if (timer) clearTimeout(timer);
}
return handleFn;
let myfun= debounce(‘要防抖的函数’, 3000); //返回的是一个函数 myfun在进行事件的绑定;