weixin_33712987 2016-04-29 06:23 采纳率: 0%
浏览 25

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

kill all ongoing XHR requests

$('#search-box').keyup(function() { // bind the search data 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条回答 默认 最新

  • 狐狸.fox 2016-04-29 06:37
    关注

    You have to do checking first, only than do filtering. Also I suggest using setTimeout to reduce server calls:

    <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
    

    To kill all request - you can try reloading page (requests will be terminated than). Or simply add some flag that indicates if you need to process further outputs or not.

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器