jquery abort() ajax request before sending another

前端 未结 4 1036
庸人自扰
庸人自扰 2020-11-29 21:54

Based on: Abort Ajax requests using jQuery... In inventory_search() - before the ajax request is made, how can I check for any current requests and abort() them before makin

4条回答
  •  [愿得一人]
    2020-11-29 22:15

    Spinon's answer is great for aborting all ajax queries but not great for keeping track of just one.

    I often find myself doing something like

    var ajax_request;
    function do_something()
    {
        if(typeof ajax_request !== 'undefined')
            ajax_request.abort();
        ajax_request = $.ajax({
            type: 'post',
            url: '/test.php',
            data: search_data,
            success: function(data) {
                $('#catalog').html(data);
            }
        });
    }
    

提交回复
热议问题