我正在尝试使用去抖函数来限制调用的滚动事件的数量。
我不确定为什么这根本不起作用......
有什么想法吗?
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 中的去抖动不适用于滚轮事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58357955/