【问题标题】:kill all ongoing XHR requests杀死所有正在进行的 XHR 请求
【发布时间】:2016-04-29 06:23:57
【问题描述】:

杀死所有正在进行的 XHR 请求

$('#search-box').keyup(function() { // 绑定搜索数据 var input = $('.search-input').val();

 $.getJSON({ // get JSON data
        url: 'example/query.php?keyword=' + input,
        //pre-load
        beforeSend: function() {
            $(".search-lists").html("<span class='loading'><img src='_/images/loading.gif' /></span>");
        },
        success: function(data) { 
            if (input.length >= 3) { // limit keyword to >=3
                var output = "<ul class='search-lists'>"; //output search data list
                $.each(data, function(key, val) {
                    output += '<li>';
                    output += '<a>' + val.term + '</a>';
                    output += '</li>';

                });
                output += '</ul>';
                $('.search-results').html(output);
                console.log('load ajax');
            } // end if

           else {
                console.log('kill ajax');
           }
        }


    }); // JSON request

}); // data bind

【问题讨论】:

  • 把你的条件放在$.getJSON({...
  • 感谢 rayon,如果输入的长度
  • @rayon 你能帮我解决最后一个问题吗
  • 你没有问问题。

标签: javascript ajax


【解决方案1】:

您必须先进行检查,而不是进行过滤。另外我建议使用setTimeout 来减少服务器调用:

<section id="search-box">
     <form class="search-field">
         <input id="search" class="search-input" type="text"  value="Hello, I'm looking for..." />
     </form>
     <div class="search-results"></div>
</section>


var timer;

$('#search-box').keyup(function() { // bind the search data
    clearTimeout(timer);

    var input = $('.search-input').val();

    if (input.length < 3) {
        return;
    }

    timer = setTimeout(function () {
        $.getJSON({ // get JSON data
            url: 'http://test.sonsuzdongu.com/query.php?keyword=' + input,
            //pre-load
            beforeSend: function() {
                $(".search-lists").html("<span class='loading'><img src='_/images/loading.gif' /></span>");
            },
            success: function(data) { 
                var output = "<ul class='search-lists'>"; //output search data list

                $.each(data, function(key, val) {
                    output += '<li><a>' + val.term + '</a></li>';
                });

                output += '</ul>';
                $('.search-results').html(output);
                console.log('load ajax');
            }
        }); // JSON request
    }, 300); //setTimeout
}); // data bind

杀死所有请求 - 您可以尝试重新加载页面(请求将被终止)。或者简单地添加一些标志来指示您是否需要处理进一步的输出。

success: function (data) {
     if (!data.isEmptyObject()) {
         // do processing.
     }
}

【讨论】:

  • 嗨,Justinas,它似乎不起作用我的控制台错误说“a.isEmptyObject”不是一个函数,你能帮我解决这个问题吗
  • @sesay console.log(typeof data, data); 说什么?
  • 这是我完整的 console.log 结果 scripts.js:1 Uncaught TypeError: a.isEmptyObject is not a functionsuccess @ scripts.js:1fire @ jquery.js:3187fireWith @ jquery.js:3317done @ jquery.js:8785(匿名函数)@jquery.js:9151
【解决方案2】:

有一个abort 方法可以取消xhr 请求。您可以根据自己的要求使用它。

【讨论】:

  • 参考这个 SO 问题link
【解决方案3】:

只需像这样存储请求,然后您可以随时中止请求。

var request = null;

 ....
 ....

 function fetchJSON (){
    if(request != null) {
        request.abort();
    }
    request = $.getJSON({ // get JSON data
            url: 'example/query.php?keyword=' + input,
            //pre-load
            beforeSend: function() {
                $(".search-lists").html("<span class='loading'><img src='_/images/loading.gif' /></span>");
            },
            success: function(data) { 
                request = null;
                if (input.length >= 3) { // limit keyword to >=3
                    var output = "<ul class='search-lists'>"; //output search data list
                    $.each(data, function(key, val) {
                        output += '<li>';
                        output += '<a>' + val.term + '</a>';
                        output += '</li>';

                    });
                    output += '</ul>';
                    $('.search-results').html(output);
                    console.log('load ajax');
                } // end if

               else {
                    console.log('kill ajax');
               }
            }


        }); // JSON request

    }); // data bind
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多