I'm using DataTables 1.10.15 in Server Side mode.
I'm passing the contents of a form to a PHP script via ajax in order for it to search a database:
var myTable = $('#myTable').DataTable( {
"processing": false,
"serverSide": true,
"searching": false,
"ajax": {
"url" : "/getData.php",
"data" : function ( d ) {
// Search input data
d.field1 = $('#field1').val(),
d.field2 = $('#field2').val(),
d.field3 = $('#field3').val()
},
"method" : "POST",
}
});
I have some other js which checks for at least 3 characters entered into a field before firing the ajax request to /getData.php
.
This means - after 3 characters have been entered - that an ajax request is made each time a key is pressed, so there can be a queue of ajax requests.
$('#primarySearch input[type="text"]').on({
"keyup": function(e) {
// Ignore tab key.
if (e.which != 9) {
processPrimarySearch.call(this);
}
}
});
/* Handle Primary Search */
function processPrimarySearch() {
var obj = $(this),
search_id = obj.attr('id'), // e.g. #field1
search_value = obj.val(); // e.g. '123-456'
/* Wait until at least 3 characters have been entered, or user has cleared the input */
if (search_value.length >= 3 || (!search_value) ) {
myTable.draw();
}
}
How can I kill off the previous ajax requests whenever a new one is made, so that they don't queue in this way? I've read How can I stop all the currently ongoing Ajax queries that DataTables instance have started? but the solution is for an older version of DataTables and the accepted answer doesn't seem to work for me.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…