本文介绍了删除去抖动回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在React商店中有以下代码:
I have the following code inside of a React store:
addChangeListener(cb) {
this.on(CHANGE_EVENT, _.debounce(cb, 100));
}
removeChangeListener(cb) {
_.debounce(cb).cancel();
this.removeListener(CHANGE_EVENT, cb);
}
我不相信 removeChangeListener $由于
_。debounce
函数,c $ c>正在删除 cb
。如何使用 _.debounce
,还要确保删除正确的 cb
?
I don't believe that removeChangeListener
is removing the cb
because of the _.debounce
function. How can I use _.debounce
, and also ensure that the proper cb
is removed?
推荐答案
你是对的。 _.debounce
创建一个新功能。所以你需要做类似的事情:
You are correct. _.debounce
creates a new function. So you need to do something like:
var debouncedCb;
addChangeListener(cb) {
debouncedCb = _.debounce(cb, 100)
this.on(CHANGE_EVENT, debouncedCb);
}
removeChangeListener(cb) {
this.removeListener(CHANGE_EVENT, debouncedCb);
_.debounce(cb).cancel();
}
根据您的具体情况,您可能希望附上 debouncedCb
以 cb
以某种方式,以便 removeChangeListener
不会混淆。
Depending on you're specifics, you'll probably want to attach debouncedCb
to cb
somehow, so that removeChangeListener
doesn't get confused.
这篇关于删除去抖动回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!