我有这个 javascript 事件监听器(因为 $(document).on('scroll...);
只是我们没有工作):
document.addEventListener("wheel", function() {}, true);
我现在有一些我想要在每次触发监听器时执行的操作,以及一些我只想在特定时间用 lodash 触发的操作。 。为了测试,我把它变得简单:
_.debounce(function() {
console.log("triggered");
}, 200);
问题是,当我将它放入单独的事件监听器中时,它会起作用。
document.addEventListener("wheel", _.debounce(function() {
"use strict";
console.log("triggered");
}, 200), true);
..但是当我将它与其余部分作为像这样的嵌套函数放在另一个中时..
document.addEventListener("wheel", function() {
"use strict";
//other code that fires every time the listener is triggered
_.debounce(function() {
console.log("triggered");
}, 200);
}, true);
..它根本不起作用。我感谢每一个想法和帮助!提前致谢!
最佳答案
_.debounce
创建一个去抖函数,但默认情况下不执行它或将其绑定(bind)到任何东西; _.debounce
是一个高阶函数,因此当您调用它时,它会返回您想要作为参数传递给监听器的函数。因此,当您在最后一个代码片段的匿名监听器函数中声明它时,它不起作用。
如果您想附加一些不像这样去抖的东西,只需单独添加它即可:
document.addEventListener("wheel", _.debounce(function() {
"use strict";
console.log("triggered");
}, 200), true);
document.addEventListener("wheel", () => {
// other code that fires every time the listener is triggered
});
关于javascript - lodash 不作为内部函数工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50015096/