jQuery-将搜索延迟到输入完成

bwoodruff1

我想延迟搜索的触发,直到用户完成输入为止。我正在使用用mark.js(https://markjs.io构建的函数它在用户键入时进行搜索,并在搜索时跳至第一个结果。问题在于它会在您键入时搜索每个字母组合。IE:搜索“帮助”搜索“ h”,“ he”,“ hel”,“ help”。考虑到文档的大小,这太慢了。我可以使用此功能做什么,以使其等待用户输入完毕?

这是我正在使用的搜索功能:

    $(function() {

  // the input field
  var $input = $("input[type='search']"),
    // clear button
    $clearBtn = $("button[data-search='clear']"),
    // prev button
    $prevBtn = $("button[data-search='prev']"),
    // next button
    $nextBtn = $("button[data-search='next']"),
    // the context where to search
    $content = $(".container"),
    // jQuery object to save <mark> elements
    $results,
    // the class that will be appended to the current
    // focused element
    currentClass = "current",
    // top offset for the jump (the search bar)
    offsetTop = 150,
    // the current index of the focused element
    currentIndex = 0;

  /**
   * Jumps to the element matching the currentIndex
   */
  function jumpTo() {
    if ($results.length) {
      var position,
        $current = $results.eq(currentIndex);
      $results.removeClass(currentClass);
      if ($current.length) {
        $current.addClass(currentClass);
        position = $current.offset().top - offsetTop;
        window.scrollTo(0, position);
      }
    }
  }

  /**
   * Searches for the entered keyword in the
   * specified context on input
   */
  $input.on("input", function() {
    var searchVal = this.value;
    $content.unmark({
      done: function() {
        $content.mark(searchVal, {
          accuracy: "exactly",
          separateWordSearch: false,
          done: function() {
//Filter Results//$context.not(":has(mark)").hide();
            $results = $content.find("mark");
            currentIndex = 0;
            jumpTo();
          }
        });
      }
    });
  });

  /**
   * Clears the search
   */
  $clearBtn.on("click", function() {
    $content.unmark();
    $input.val("").focus();
  });

  /**
   * Next and previous search jump to
   */
  $nextBtn.add($prevBtn).on("click", function() {
    if ($results.length) {
      currentIndex += $(this).is($prevBtn) ? -1 : 1;
      if (currentIndex < 0) {
        currentIndex = $results.length - 1;
      }
      if (currentIndex > $results.length - 1) {
        currentIndex = 0;
      }
      jumpTo();
    }
  });
});

以下是一些HTML:

<input type="search" placeholder="Search" class="text-input" style="height: 28px; border-style: solid; border-color: black; border-width: 1px; margin-bottom: 3px;">
      <br/>
      <button data-search="next">Next</button>
      <button data-search="prev">Previous</button>
      <button data-search="clear">✖</button><br/>
<div class="container">
   <div>
     Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus elementum non arcu id tristique. Cras fringilla nisi erat, non ultricies leo varius eu. Duis venenatis enim sed tristique gravida. Nulla dolor augue, tempor non nisl eget, consequat consequat magna. Nulla quis elit nisl. Integer at tortor molestie, dignissim odio id, pharetra nunc. Maecenas scelerisque porta erat bibendum aliquet. Proin est felis, pretium sit amet consequat eu, aliquet ac erat. Donec tempus, libero sit amet rutrum posuere, leo ipsum gravida odio, quis sodales magna nisl ut lectus.
   </div>
</div>
查理

您可以使用计时器限制搜索功能的初始化,还可以检查是否存在最少字符数

就像是:

var searchTimer = null,
  minLength = 3,
  searchDelay = 300;

$input.on("input", function() {
  // clear previous timer
  clearTimeout(searchTimer);

  var searchVal = this.value;

  // don't do anything if not enough characters
  if (searchVal.length < minLength) {
    return;
  }  

  // start new timer
  searchTimer = setTimeout(function() {
    $content.unmark({
      done: function() {
        $content.mark(searchVal, {
          accuracy: "exactly",
          separateWordSearch: false,
          done: function() {
            //Filter Results//$context.not(":has(mark)").hide();
            $results = $content.find("mark");
            currentIndex = 0;
            jumpTo();
          }
        });
      }
    });
  }, searchDelay);
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

jquery 自动完成搜索输入

将jquery脚本延迟到其他所有内容都加载完毕

如何将渲染延迟到promise的then()完成之前?

JavaScript jQuery输入延迟

如何添加延迟到jQuery .addClass和.removeClass?

将排序或搜索设置添加到JQuery自动完成?

如何将jQuery自动完成附加到输入元素

RxJs | 如何将`mergeMap`延迟到先前的内部可观察值完成的时刻?

如何将悬停的重置延迟到转换完成后?

如何将方法延迟到NSData完成在NSUrlSession中的加载?

Kafka-将绑定延迟到复杂服务初始化完成之前

jQuery UI自动完成-在KEYDOWN / KEYUP之后访问输入的搜索词

如何在JQUERY中过滤搜索输入以显示正确的自动完成功能?

jQuery自动完成功能不将结果限制为输入控件中的搜索值

jQuery延迟切换搜索栏

jquery选择输入字段搜索

完成输入后将保存带有输入的ASP.NET jQuery Ajax表

如何使用jQuery自动完成功能来搜索特定记录而忽略在输入框中输入的顺序词

将服务呼叫延迟到循环之后

在Azure Logic App中无法使用``延迟到''完成时间

将输入ID传递给jQuery UI自动完成功能中的url

如何将输入字段设置为空,在jquery中用作自动完成?

jQuery自动完成设置隐藏的输入值

jQuery自动完成和复制的输入字段

jQuery自动完成输入密钥问题

PHP的jQuery自动完成与动态输入字段?

选择时的jQuery自动完成清除输入

将延迟应用于动画jquery

jQuery $.when 不等待延迟链完成 ajax 调用