gpt4 book ai didi

javascript - 在 Javascript 中去抖动不适用于 wheel 事件

转载 作者:行者123 更新时间:2023-11-30 13:50:29 25 4
gpt4 key购买 nike

我正在尝试使用去抖功能来限制调用的滚动事件的数量。

我不确定为什么这根本不起作用......

有什么想法吗?

window.addEventListener('wheel', () => {
debounce(scrollSection, 300);
});

const scrollSection = () => {
console.log(1);
}

const debounce = function(fn, d) {
let timer;
return function() {
let context = this;
let args = arguments;
clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(context, args);
}, d);
}
}

最佳答案

它在每个 wheel 事件上创建去抖函数。先去抖一个函数,然后把它放到事件监听器中。

window.addEventListener('wheel', debounce(scrollSection, 300));

const scrollSection = () => {
console.log(1);
}

const debounce = function(fn, d) {
let timer;
return function() {
let context = this;
let args = arguments;
clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(context, args);
}, d);
}
}

关于javascript - 在 Javascript 中去抖动不适用于 wheel 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58357955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com