function throttle(fn, interval) {
let last = 0
return function () {
const context = this
const args = arguments
const now = +new Date()
if (now - last >= interval) {
fn.apply(context, args)
last = now
}
}
}
function debounce(fn, delay) {
let timer = null
return function () {
const context = this
const args = arguments
if(timer) {
clearTimeout(timer)
}
timer = setTimeout(()=>{
fn.apply(context,args)
timer = null
},delay)
}
}
const betterScroll = debounce(() => {
console.log('滚动了')
}, 1000)
document.addEventListener('scroll', betterScroll)
body {
height: 2000px;
}
console