5

Ok i have a function like

So when this function is called the AJAX request is made but if I again call the same function. The current request is made but the previous call is not killed. I want the datatable to kill any previous calls and run only the latest one.

I'm not sure how to do this.

Anonymous
  • 145
  • 1
  • 4
  • 14

5 Answers5

8

This is how I do it with DataTables 1.10.16

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

It might help someone

        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();
                }
            }
        }
DoubleK
  • 542
  • 3
  • 16
2

The following worked for me...

var elTable = jQuery('table#myDataTable');
var oTable = elTable.DataTable();
oTable.context[0].jqXHR.abort()
ScottyG
  • 3,204
  • 3
  • 32
  • 42
1

I think these threads will be helpful:

Abort previous ajax call in datatables 1.10

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

It looks like you could add something like this to the first line of your function:

function load(id) {
    if($('#videos_list').DataTable().settings.jqXHR) {
        $('#videos_list').DataTable().settings.jqXHR.abort()
    }
    $("#table_wrapper").mask("");
    etc...
Community
  • 1
  • 1
Adam
  • 2,446
  • 1
  • 13
  • 16
1

I have Datatable 1.10.5 . Its worked.

           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();
               }
           }
       }

I also did the call delay as follows.

       var $searchBox = $(tableId + "_filter input[type='search']");
       $searchBox.off();

       var searchDebouncedFn = debounce(function () {
           $(tableId).DataTable().search($searchBox.val()).draw();
       }, 300);

       $searchBox.on("keyup", searchDebouncedFn);
   }

   /* from https://davidwalsh.name/javascript-debounce-function */
   function debounce(func, wait, immediate) {
       var timeout;
       return function () {
           var context = this, args = arguments;
           var later = function () {
               timeout = null;
               if (!immediate) func.apply(context, args);
           };
           var callNow = immediate && !timeout;
           clearTimeout(timeout);
           timeout = setTimeout(later, wait);
           if (callNow) func.apply(context, args);
       };
   };

   debounceSearch('#table') ```