参考:https://blog.csdn.net/qq_35585701/article/details/81392174
https://www.jianshu.com/p/b7b698db8352
防抖:
function debounce (fn, wait = 1000) {
let timeOutId = null;
return function () {
let context = this;
clearTimeout(timeOutId)
timeOutId = setTimeout(() => {
fn.apply(context, arguments)
}, wait)
}
}
节流:
function throttle(fn, delay, immediate) {
var timer = null;
return function () {
const that = this;
if (!timer) {
timer = setTimeout(() => {
fn.call(that, arguments);
clearTimeout(timer);
}, delay)
}
}
}