0

I have a web app that occasionally (after some idleness) will block a very simple request for small chunks of data (30~50 kb) up to 20 or so seconds. Assuming I can't refactor or modify the API, is there some pattern in javascript or jquery to accommodate a situation like this?

I'm thinking to set a timeout for 5 seconds or so for the api call and retry the ajax request -- via jquery's $.ajax() with the timeout argument. I imagine this could be the equivalent of the user refreshing a slow-loading page. Thanks for your thoughts.

  • 1
    Are you using synchronous ajax calls? Asychronous ajax calls should never block. It is generally bad form to blindly auto-retry something (without special back-off logic) that is going slowly as this can lead to avalanche failure. System is slow so responses are slow so all clients start bailing and auto-retrying which makes system even worse and things get worse and worse (thus called an avalanche failure - the worse the system is the worse the clients treat it, the worse the system gets until failure). – jfriend00 Oct 15 '13 at 23:05
  • @jfriend00 My fault for mis-stating the problem. Yes, I'm using asynchronous ajax calls to fetch the data. The api is what's blocking: by which I mean occasionally it just takes an absurdly long time to return the data (20 seconds) -- even though the sql query on the back end and the amount of data returned are trivial. – b.b. Oct 15 '13 at 23:07
  • See the rest of my comment. Blindly retrying things because they are slow can lead to more trouble. – jfriend00 Oct 15 '13 at 23:08
  • Interesting thoughts about the avalanche problem. Maybe there is a way to set up the client to retry fetching the data but then also to give it up after 2 or 3 re-tries -- so as not to become too abusive. – b.b. Oct 15 '13 at 23:08
  • 1
    The best course of action actually depends upon why things get slow sometimes and what is causing that. Even if you can't fix that, knowing why it does that would give you a better idea on how to adapt. – jfriend00 Oct 15 '13 at 23:09
1

jQuerys .ajax() mathod is asynchronous by default meaning the user can still use the page. To prevent users from getting confused or impatient, they need to know that the page is loading. jQuery gives us hooks to show a loading message:

$.ajax({
    url: 'service.php',
    success: function(){
        //display results
    },
    beforeSend: function(){
        // start an unobtrusive progress bar or spinner
    }
    complete: function(){
        // hide the progress bar/spinner
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.