I have a web app that occasionally (after some idleness) will block a very simpl
ID: 647019 • Letter: I
Question
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.
Explanation / Answer
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
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.