防抖
效果:短时间内一直触发事件时,只执行最后一次触发的函数
代码:借助setTimeout
来实现,当下次触发事件时,如果setTimeout
已经存在,则清除setTimeout
并重新计算时间
/*
fn: 要执行的函数
delay: 延迟执行时间ms
args: fn函数的参数(数组)
context: fn函数的this指向(默认是函数执行时的上下文环境)
*/
function debounce(fn, delay, args, context) {
let timer = null;
return function() {
context = context || this;
args = args || arguments;
if(timer != null) {
clearTimeout(timer);
}
timer = setTimeout(function() {
fn.apply(context, args);
}, delay)
}
}
function handle()