console
function throttle(fn, s, duration) {
let timer
let beginTime = new Date()
return function() {
let _this = this
let args = arguments
let now = new Date()
clearTimeout(timer)
if(now - beginTime > duration) {
fn.apply(_this, args)
beginTime = now
} else {
timer = setTimeout(() => {
fn.apply(_this, args)
}, s)
}
}
}
function scrollFn() {
console.log('检测到滚动事件')
}
window.onscroll = throttle(scrollFn, 500, 1000)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<div style="background-color: lightblue; height: 200vh;"></div>
</body>
</html>