function dedounce(fn, duration) {
let timeId;
// 此处的this指向windows
return function () {
// 此处的this指向调用函数的对象
const that = this;
if (timeId) {
clearTimeout(timeId);
}
// 这个地方的函数是事件函数
const args = Array.prototype.slice.call(arguments, 0);
timeId = setTimeout(function () {
fn.apply(that, args)
}, duration)
}
}
input.addEventListener('keyup', dedounce(function (e) {
span.innerHTML = this.value;
}, 1000));
标签:function,防抖,const,函数,js,timeId,duration
From: https://www.cnblogs.com/ccblblog/p/18133960