Search code examples
ajaxsingle-page-applicationdurandal

Pending AJAX requests in a SPA freeze the app


In my durandal application I have a number of polling requests happening every 10 seconds. But at times if the server stops responding to that polling the requests stays in a pending state. If there are more pending requests than the browser supports, it freezes the application and requires a browser refresh.

I have combined the polls as much as possible and made sure that the a new request is made only after the previous one is complete. But still I am having many number of parallel polls. Also, switching to sockets is not easy at this point.

Would like to know what are the best practices to avoid these kind of issues in the SPA world.


Solution

  • You should be able to prevent an unbounded number of parallel ajax requests by chaining them together recursively. Let's say you have two parallel ajax requests, you can do this:

    function doCallOne() { 
        $.get('urlOne').done(doCallOne);
    }
    function doCallTwo() { 
        $.get('urlTwo').done(doCallTwo);
    }
    doCallOne();
    doCallTwo();
    

    Now, you will have calls that continuously chain after the previous call resolves. You can further extend these functions to yield a minimum delay, to be manually exited, and so on. What will never happen, however, is for call one to have more than one pending call.