如何杀死DataTables中以前的AJAX调用

时间:2016-05-12 12:50:28

标签: jquery ajax datatables

好的,我有一个像

这样的功能

因此,当调用此函数时,会发出AJAX请求但是如果我再次调用相同的函数。当前请求已完成但前一次调用未被终止。我希望数据表能够杀死任何先前的调用并仅运行最新的调用。

我不知道该怎么做。

5 个答案:

答案 0 :(得分:3)

  

这就是我使用DataTables 1.10.16

的方法
var table = $('#table').DataTable({
    ajax: {
        url: '/datatable',
        type: 'POST',
        beforeSend: function() {
            if (table.hasOwnProperty('settings')) {
                table.settings()[0].jqXHR.abort();
            }
        }
    }
});

答案 1 :(得分:1)

我认为这些主题会有所帮助:

Abort previous ajax call in datatables 1.10

How can I stop all the currently ongoing Ajax queries that DataTables instance have started?

看起来你可以在你的函数的第一行添加这样的东西:

function load(id) {
    if($('#videos_list').DataTable().settings.jqXHR) {
        $('#videos_list').DataTable().settings.jqXHR.abort()
    }
    $("#table_wrapper").mask("");
    etc...

答案 2 :(得分:1)

以下对我有用......

var elTable = jQuery('table#myDataTable');
var oTable = elTable.DataTable();
oTable.context[0].jqXHR.abort()

答案 3 :(得分:0)

这可能会帮助某人

        preDrawCallback: function(settings) {
            if ($.fn.DataTable.isDataTable('#table')) {
                var dt = $('#table').DataTable();

                //Abort previous ajax request if it is still in process.
                var settings = dt.settings();
                if (settings[0].jqXHR) {
                    settings[0].jqXHR.abort();
                }
            }
        }

答案 4 :(得分:0)

我有数据表 1.10.5 。它的工作。

at Object.mkdirSync (fs.js:921:3)
at AsyncFunction.t.exports.sync (/usr/local/lib/node_modules/@sanity/cli/bin/sanity-cli.js:3122:1189)
at t.exports.set all [as all] (/usr/local/lib/node_modules/@sanity/cli/bin/sanity-cli.js:3116:828)
at new t.exports (/usr/local/lib/node_modules/@sanity/cli/bin/sanity-cli.js:3116:534)
at a (/usr/local/lib/node_modules/@sanity/cli/bin/sanity-cli.js:508:324)
at e.default (/usr/local/lib/node_modules/@sanity/cli/bin/sanity-cli.js:2580:708)
at t.exports (/usr/local/lib/node_modules/@sanity/cli/bin/sanity-cli.js:1788:1599)
at Object.<anonymous> (/usr/local/lib/node_modules/@sanity/cli/bin/sanity-cli.js:1782:134)
at n (/usr/local/lib/node_modules/@sanity/cli/bin/sanity-cli.js:2:124)
at /usr/local/lib/node_modules/@sanity/cli/bin/sanity-cli.js:2:923

我也做了如下调用延迟。

           if ($.fn.DataTable.isDataTable('#table')) {
               var dt = $('#table').DataTable();

               //Abort previous ajax request if it is still in process.
               var settings = dt.settings();
               if (settings[0].jqXHR) {
                   settings[0].jqXHR.abort();
               }
           }
       }