如何结束和执行去抖?反应

问题描述

我有一个使用 lodash's debounce 延迟搜索输入的工作代码。但是我不知道如何结束并在另一个函数中执行它。

逻辑是如果我在搜索栏中输入,它应该等待 5 秒才能改变。但是如果我点击添加名称按钮,它应该结束等待/去抖动并直接更改它。

我访问过此链接,但不知道如何将其应用于我的场景 How to cancel a debounced function after it is called and before it executes? 。 有人知道怎么做吗?谢谢


这是我的工作代码

const debounce = _.debounce;

const onChangeName = debounce((query,id) => {
    names[id] = query.target.value;
    setNames(names);
},5000);

const onAddName = () => {
    // <=== I want here to end the debounce,and automatically execute changing of name
    setNames(names.concat(['']));
};

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)