【问题标题】:debounce the first function call in lodash/debouncedebounce lodash/debounce 中的第一个函数调用
【发布时间】:2019-04-08 12:23:10
【问题描述】:

我正在寻找的东西非常简单,但我已经坚持了一段时间:
我一直在使用 lodash 的 debounce 功能,以便在您键入功能时实现搜索。
基本上,当您在搜索栏中输入内容时,网站会在最后一次键盘输入后等待 500 毫秒,然后才会触发搜索。

问题是第一次调用没有去抖,因为去抖会等待第二次调用以引入延迟。

现在我已经使用选项{ trailing: true, leading: false } 对其进行了配置,但我不知道如何配置它以使第一个函数调用去抖动。

const DebouncedSearchBox = ({ currentRefinement, refine }) => {
  const debouncedSearch = debounce(
    e => {
      refine(e.target.value);
      if (!e.target.value.length) {
        document.getElementsByClassName("ais-Pagination-list")
          ? Array.from(
              document.getElementsByClassName("ais-Pagination-list")
            ).forEach(function(element) {
              element.classList.add("hidden");
            })
          : null;
        document.getElementsByClassName("ais-Stats-text")
          ? Array.from(
              document.getElementsByClassName("ais-Stats-text")
            ).forEach(function(element) {
              element.classList.add("hidden");
            })
          : null;
      } else {
        document.getElementsByClassName("ais-Pagination-list")
          ? Array.from(
              document.getElementsByClassName("ais-Pagination-list")
            ).forEach(function(element) {
              element.classList.remove("hidden");
            })
          : null;
        document.getElementsByClassName("ais-Stats-text")
          ? Array.from(
              document.getElementsByClassName("ais-Stats-text")
            ).forEach(function(element) {
              element.classList.remove("hidden");
            })
          : null;
      }
      document.getElementsByClassName("search_results")
        ? Array.from(
            document.getElementsByClassName("search_results")
          ).forEach(function(element) {
            element.classList.remove("loading");
          })
        : null;
    },
    500,
    { trailing: true, leading: false }
  );

  const onChange = e => {
    e.persist();
    console.log("on change" + e.target.value);
    document.getElementsByClassName("search_results")
      ? Array.from(
          document.getElementsByClassName("search_results")
        ).forEach(function(element) {
          element.classList.add("loading");
        })
      : null;
    debouncedSearch(e);
  };

  return (
    <input
      defaultValue={currentRefinement}
      onChange={onChange}
      aria-label="recherche"
      className="ais-SearchBox-input"
      autoFocus
      onFocus={e => {
        let val = e.target.value;
        e.target.value = "";
        e.target.value = val;
      }}
    />
  );
};

【问题讨论】:

  • 请发布您的代码。
  • @DerekBrown 当然

标签: javascript lodash


【解决方案1】:

对于独立的debouncing 函数,您可以为立即布尔值添加第三个参数,以验证该函数应在前沿立即执行,然后等待。

例如:

/**
* debounce
* @type {Function}
*
* @param {Function} cb
* @param {Number} wait
* @param {Boolean} immediate
*
* @return {Function} debounced function
*/

const debounce = (cb, wait, immediate) => {
  let timeoutId;

  return () => {
    const context = this;
    const args = arguments;

    if (immediate) {
      cb();
    }

    clearTimeout(timeoutId);

    timeoutId = setTimeout(() => cb.apply(context, args), wait);
  }
}

对于您当前的实现,也许将{ leading: false} 标志切换到true 应该可以触发前沿。

【讨论】:

  • 嘿,感谢您的回答!实际上我刚刚发现我无法解决这个问题,因为 UX 原因太长而无法在这里公开。所以我改变了我的界面,现在它工作得更好了。
猜你喜欢
  • 2021-08-11
  • 1970-01-01
  • 1970-01-01
  • 2016-08-10
  • 2019-12-21
  • 2014-08-09
  • 1970-01-01
  • 1970-01-01
  • 2020-01-07
相关资源
最近更新 更多