我有一个使用 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(['']));
};