// 防抖
const debounce = (fn, timeout) => {
let timer
return function (...args) {
const context = this;
if (timer) clearTimeout(timer)
timer = setTimeout(() => {
fn.call(context, ...args)
}, timeout)
}
}
// 节流
const throttle = (fn, timeout) => {
let timer
return function (...args) {
const context = this
if (timer) return
timer = setTimeout(() => {
fn.call(context, ...args)
clearTimeout(timer)
timer = null
}, timeout)
}
}
function onClick(e) { console.log(e) }
// document.addEventListener('click', debounce(onClick, 300))
document.addEventListener('click', throttle(onClick, 500))
console