Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
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. –  BBnyc 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. –  BBnyc 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
show 2 more comments

1 Answer 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
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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