<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
body {
height: 2000px;
}
</style>
<body>
<!--防抖:对于短时间内多次触发事件的情况,可以使用防抖停止事件持续触发 -->
<h2>防抖</h2>
<script>
let timer = null;
window.onscroll = function () {
if (timer !== null) {
clearTimeout(timer);
}
timer = setTimeout(() => {
console.log("hello");
timer = null;
}, 1000)
}
</script>
</body>
</html>