admin管理员组文章数量:1430592
My question is, if there is a way to cancel lodash's debounce, without invoking the function to debounce.
The .cancel()
from lodash still invokes the function.
Maybe I am looking for the wrong approach. If so, is there any other solution for something like this?
Thank you in advance.
My question is, if there is a way to cancel lodash's debounce, without invoking the function to debounce.
The .cancel()
from lodash still invokes the function.
Maybe I am looking for the wrong approach. If so, is there any other solution for something like this?
Thank you in advance.
Share Improve this question asked Jul 31, 2020 at 14:00 philmanphilman 1593 silver badges12 bronze badges1 Answer
Reset to default 6Calling the cancel
method on the debounced function does not invoke it, unless it is on the trailing edge of the timeout. You can use the trailing
option to configure this. Docs
const fn1 = () => console.log('Called 1');
const fn2 = () => console.log('Called 2');
const fn3 = () => console.log('Called 3');
const debouncedFn1 = _.debounce(fn1, 1000);
const debouncedFn2 = _.debounce(fn2, 1000);
const debouncedFn3 = _.debounce(fn3, 1000, { trailing: false });
// Will not log
debouncedFn1();
setTimeout(debouncedFn1.cancel, 200);
// Will log
debouncedFn2();
setTimeout(debouncedFn2.cancel, 1000);
// Will not log
debouncedFn3();
setTimeout(debouncedFn3.cancel, 1000);
<script src="https://cdn.jsdelivr/npm/[email protected]/lodash.min.js"></script>
本文标签: javascriptCancel lodash39s debouncewithout invoking the functionStack Overflow
版权声明:本文标题:javascript - Cancel lodash's debounce, without invoking the function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745505484a2661224.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论