Preventing multiple active AJAX queries

Preventing multiple active AJAX queries

cavaccavac Posts: 5Questions: 2Answers: 0

When i type in the search box, i often see multiple active AJAX queries at the same time (on some database tables, it takes a couple of seconds to filter the results due to the many millions of entries).

As happens in this kind of multithreading environment, sometimes a later query returns before the earlier one. This leads to the effect that the UI updates twice, the second time with the wrong data.

Is there any way i can defer further updates/AJAX queries until the first AJAX call has finished. I don't need a queue per se, only a flag that is checked on return of a query to say "update screen, but automatically run another query with updated filters". This flag should also prevent sending further queries while one is still outstanding.

I looked through the documentation and well as doing some Google searches, but came up empty. (Though i admit i might have overlooked something).

This question has accepted answers - jump to:

Answers

  • cavaccavac Posts: 5Questions: 2Answers: 0

    Just to explain why this happens: Consider a Table with 1 million entries. The user wants to search for "59".

    They type "5", the server finds 100.000 entries in the index and has load them from disk and then to do an "ORDER BY", which takes a while. While that is running, the user types "9". The seconds query only finds 500 entries in the index, so has to load and sort much less data and can potentially return first.

  • rduncecbrduncecb Posts: 125Questions: 2Answers: 28
    Answer ✓

    Assuming you're using serverSide: true, the protocol(see Sent parameters, https://datatables.net/manual/server-side) should make sure ajax responses are processed in the order they are made even if responses are out of order. I'm just a user so in no place to say if it's working correctly but I am planning to utilise the protocol for some REST calls so am an interested party ;). If you've implemented your own back-end for serverSide processing are you sending back the draw parameter that gets made with the request so datatables can make sure it stays in sync?

  • cavaccavac Posts: 5Questions: 2Answers: 0
    edited September 2017

    Ah yes, "draw" order... ooops, yeah, casting that to a number( just as the documentation says) solved the immediate problem. facepalm

    It still does somewhat to much queries, but filtering now seems to work.

    This might be noteworthy for future reference: If you do something wrong (sending a string) when sending the draw order from the server, future requests from DataTables to the server set the value to 'NaN'.

    Added some checks for that in my code as well.

    Thanks for the quick help!

  • rduncecbrduncecb Posts: 125Questions: 2Answers: 28
    edited September 2017 Answer ✓

    Glad I could point you in the right direction!
    As for the amount of queries, you could turn off the default search box event handler as seen in this example from @bindrid - http://live.datatables.net/zazususu/1/edit
    and replace it with an event handler that would debounce the user input and calls to search()

    Cheers for the heads up on making sure the draw parameters comes back as an number, I'll keep it in mind when I implement my server side processing in the future :smiley:

  • cavaccavac Posts: 5Questions: 2Answers: 0

    Shame there is only a "drawCallback", but no ajaxStarted/ajaxFinished callbacks to effectively do a limiter based on the number of outstanding AJAX requests.

    In my case, using the drawCallback in combination with your code to implement a "maximum of 2 outstanding requests" policy should work reasonably well.

    But in my mind it isn't a clean solution, because there could be a number of reasons of drawCallback happening without finishing an AJAX callback (artificially "cleaning out" the counter), especially when using plugins. I might be wrong there...

This discussion has been closed.