首页 > 解决方案 > lodash debounce 忽略空格键

问题描述

嗨,我正在与 lodash 合作 .. 一切都很好 .. 但我有一个问题,那就是当按下 enter 键时我希望 debouce 不工作 .. 这是我的 lodash 键 ..

search_products:_.debounce(function(event)
{
     // my code here 
    // how can i let debounce work with all keys but not with enter key
},5),

如果有任何其他方法,如 debouce 或任何花药库,我也希望延迟 5ml 不能与 enter 一起使用,任何人都可以在这里帮助我,谢谢

标签: javascriptlodash

解决方案


您可以提取 debounced 函数,并且仅在按下回车键以外的其他内容时调用它。

{
  search_products: function (event) {
    if (event.code !== 'Enter') {
      debounced(event);
    }
  }
}

const debounced = _.debounce(function(event) {
  // ...
}, 5);

推荐阅读