javascript - Javascript 中的去抖动不适用于车轮事件
问题描述
我正在尝试使用去抖动功能来限制调用的滚动事件的数量。
我不知道为什么这根本不起作用......
有任何想法吗?
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 - NodeJS:表格元素的 Href
- python - Python:从 arg 解析器到字典
- django - 嵌套序列化程序的 Yasg 模式生成显示不正确的请求/响应
- vue.js - Vue SSR 子级别路由
- python - 使用熊猫有条件地移动列中的数据
- c++ - 如何在 DevC++ 中访问 Zlib.h?
- node.js - Readline操作的Node JS代码说明
- javascript - 为什么我在 addEventListener 中的函数只执行所写内容的一部分?
- cuda - Cuda min warp reduction 产生竞争条件
- image - 图像不显示在 reStructuredText (.rst) - HTML - 在 VS Code 中预览