<script>
// 节流,如果连续触发事件,则每隔一段时间才能触发一次事件
let box = document.querySelector('div')
box.addEventListener('click', throttle(1000))
//定义一个节流阀 类似于开关
function throttle(data) {
let timer = null;
return add => {
if (!timer) {
clearTimeout(timer);
timer = setTimeout(() => {
console.log(11111111);
timer = null;
}, data);
}
}
}
// 防抖 只在最后一次事件停止后触发
function throttle(data) {
let timer = null;
return add => {
clearTimeout(timer);
timer = setTimeout(() => {
console.log(11111111);
}, data);
}
}
</script>
节流和防抖
最新推荐文章于 2025-05-28 19:34:53 发布