can draw be cancelled in preDraw.dt handler?
can draw be cancelled in preDraw.dt handler?
in DataTables 2
I'm trying to resolve a race condition between an interval timed draw and some other user actions. Claude AI suggests returning false in the preDraw.dt handler, but for whatever reason I'm not finding that in your documentation. Will this work? (I'm using 1.10, but will also move to 2.x soon)
// preDraw.dt (camelCase) is the correct DataTables event name; returning false cancels the draw.
_dt_table.on('preDraw.dt', function() {
if (scan_mousedown) {
console.log('preDraw.dt: draw blocked by scan-action-btn guard');
return false;
}
});
This question has an accepted answers - jump to answer
Answers
This is from the docs:

thanks, not sure how I missed that
Easy done!
The "Please note" in the documentation there is quite important. By the time
preDrawtriggers, the sorting and filtering will already have happened, so if you interact with the API after that event, and the draw has been cancelled, the data might not align with what is still shown (i.e. ordering might have changed, etc).You need to be really careful if you use this!
Rather than cancelling a draw, I'd be tempted to put in a flag around the user action which will indicate to the interval timer that it shouldn't update the table until the user action has completed and any subsequent data updates made.
Regards,
Allan
Rather than cancelling a draw, I'd be tempted to put in a flag around the user action which will indicate to the interval timer that it shouldn't update the table until the user action has completed and any subsequent data updates made.
This is being done, too, but there's a small window where the draw was initiated then the user action occurs before the response is received. Actually for convenience this flag is being checked when the draw interval expires, which opens the window somewhat. See scan_mousedown.
Actually I'm not sure I'm clear on the case where ordering and filtering may have been done. Maybe this wouldn't happen with a periodic draw? I can see though that if the user is doing filtering or ordering the preDraw event will occur. I imagine if the user clicks on one of the protected buttons right after filtering a draw would be missed, but it would be picked up after the next interval?
If you cause the table to draw, it will go through the sort, filter, preDraw and then draw actions. The table draw is a synchronous action unless you are using Ajax or server-side processing (looks like you are using at least Ajax).
Correct. The next draw would bring everything back into sync. That includes if the draw was triggered by the end user.
Allan