直接上代码
function throttle(fun, delay) {
let oldTime = 0,
t = null;
return function () {
if (t) {
clearTimeout(t);
}
let args = [...arguments],
_this = this,
newTime = +new Date();
if(newTime - oldTime >= delay) {
fun.apply(_this, args);
oldTime = +new Date();
} else {
t = setTimeout(() => {
fun.apply(_this, args)
oldTime = +new Date();;
}, delay)
}
}
}