【问题标题】:How to add a delay to an ajax request to avoid performance issues如何向 ajax 请求添加延迟以避免性能问题
【发布时间】:2016-04-05 13:32:57
【问题描述】:

我有一个函数,它在输入时返回一定数量的 AJAX 触发数据。每次它都会去 Oracle DB 获取数据并显示输出。

但是,一旦我输入“你好,我的名字是”这样的“长”字符串,每次我输入一个键时它都会启动一个请求(一次 10 个请求),并且需要 5 秒才能得到 1请求响应。

这导致我的应用程序出现性能问题。请问我该如何避免呢?

我已经尝试过不起作用的动态和延迟。我正在使用 typeahead 2 (typeahead-bs2.min.js)。

$( "#customer_description" ).typeahead({

 dynamic: true,
 delay: 5000,
 source: function (query, process) {
      $.ajax({
           url: '/data/customer.php',
           type: 'POST',
           dataType: 'JSON',
           //contentType: "text/json; charset=utf-8",                        
           data: {
                       all: query,
                   },
            success: function(data) {
                //console.log(data);
                process(data);
            }
        });
    }
});

【问题讨论】:

  • 延迟没有帮助。稍后它仍会执行 10 次查询。考虑输入。在进行查询之前字符串是否应该匹配某些条件,例如最小长度,或者可能在输入完成时运行查询等。
  • 这是哪个版本的预输入?你能用a more recent version吗?

标签: jquery ajax typeahead.js


【解决方案1】:

一旦用户停止输入,您可以在 1.5/2 秒间隔后触发请求,并按照Sami 的建议检查输入长度。

除此之外,为了避免发送多个请求,您始终可以中止前一个请求并发送一个新请求。

 var start = new Date()
 ,end, request;

 $('input:text').on('change', function(){
  end = new Date();
  var input = $(this).val();
  var diff = (end - start)/1000;

  //checking for a 1.5 sec delay and input length
  //You can adjust the delay time, I have kept it as 1.5seconds
  if(diff > 1.5 && input.length > 5){
    if (request !== undefined){
      //aborting the previous request
      request.abort();
    }
    start = new Date();

    //storing the ajax request in a variable so that it can be aborted later when we fire a new request

    request = $.ajax({
      url: '/data/customer.php',
      type: 'POST',
      dataType: 'JSON',
      //contentType: "text/json; charset=utf-8",                        
      data: {
      all: query,
      },
      success: function(data) {
               //console.log(data);
                 process(data);
              }
    });
   }
 });

【讨论】:

    【解决方案2】:

    如果我没记错的话,您是说您有类似搜索输入的东西会触发 Ajax 调用,并且您希望阻止该 Ajax,以便它不会在您按下的每个键时触发。我说的对吗?

    类似:

        var timeOutId;
        $("#customer_description").typeahead({
            dynamic: true,
            source: function (query, process) {
                //if there's no ajax call running (the timeout was cleared)...
                if (!timeOutId || 'undefined' === typeof timeOutId) {
                    timeOutId = setTimeout(ajaxCall, 300)
                }
            }
        });
    
        function ajaxCall() {
            $.ajax({
                url: '/data/customer.php',
                type: 'POST',
                dataType: 'JSON',
                //contentType: "text/json; charset=utf-8",                        
                data: {
                    all: query,
                },
                success: function (data) {
                    //console.log(data);
                    process(data);
                },
                complete: function(){
                  //when the ajax finishes (success or not) clear the interval
                  clearTimeout(timeOutId);
                }
            });
        }
    

    您可以通过“静态类”(具有函数的对象)或伪类来改进此代码,但这取决于您。通过这种方式,您可以更改标志或其他内容的超时时间,您可以添加字符串长度检查...或您能想到的任何内容。

    【讨论】:

    • 您好 Astaroth,感谢您的宝贵回复。但是当我使用这种方法时,变量“查询”的范围没有正确验证。我尝试了一些方法来定义这些变量,但是我没有得到想要的输出。如果您能帮我解答这个问题,将不胜感激。提前致谢。
    • 您可以编辑主帖并显示您删除该查询变量的代码吗?也许只是你需要在更高的范围内声明它。
    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 2018-01-02
    • 2016-12-12
    • 2021-06-22
    • 2012-02-03
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    相关资源
    最近更新 更多