Polling Ajax: options other than setInterval?

Polling Ajax: options other than setInterval?

GregPGregP Posts: 500Questions: 10Answers: 0
edited January 2016 in Free community support

Using straight-up Ajax calls in my applications, I tend to use setTimeout rather than setInterval for polling. This way there is no risk of requests coming back out of order. The Ajax request is fired, and in the success callback you setTimeout for another Ajax request.

Since DataTables' Ajax functionality takes advantage of the success handler in the XHR, it's very risky to try to override it. Or at least that's my current understanding. Is there a more convenient place to "simulate" this behaviour? Ideally it would be something like this:

    myApp.table = $('#sometable').on('error.dt', someErrorHandler).DataTable({
      "ajax": {
        "url": "/rest/somedata",
        "dataSrc": function (json) {
          return fcr.utils.wrapArray(json.subObject);
        }
      },
      "someQuasiSuccessThing": function() {
        setTimeout(function() { myApp.table.ajax.reload(); }, 1000);
      }
    });

The setInterval method isn't completely written off, but (perhaps more at an aesthetic level rather than practical level) the idea of blindly firing the request without knowing if the previous one returned is irksome for me.

Thanks!

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,761Questions: 1Answers: 10,510 Site admin
    Answer ✓

    You could listen for xhr and trigger your setTimeout there.

    Allan

  • GregPGregP Posts: 500Questions: 10Answers: 0

    Perfect, thanks Allan!

This discussion has been closed.