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

时间:2016-04-29 06:23:58

标签: javascript ajax

终止所有正在进行的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

3 个答案:

答案 0 :(得分:0)

你必须先做检查,而不是做过滤。另外,我建议使用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.
     }
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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
 }
相关问题