This question deals with concurrency issues, for suggestions on how to display a busy icon see this question: http://stackoverflow.com/questions/205631/javascript-loadingbusy-indicator-or-transparent-div-over-page-on-event-click
When a user initiates an AJAX request on a page it's useful to show some kind of "working" or busy icon or progress indicator. If there is only one long-running process this can be handled in a relatively straightforward way:
function do_action() {
show_busy_icon();
long_running_asynchronous_process(function() {
// Callback function run when process finishes
hide_busy_icon();
});
}
However, if multiple asynchronous processes are running on the page, using on/off methods wouldn't work. The first process to finish switches off the icon even though there are additional processes running.
So, how do you handle displaying an indicator, on a web page, that is on when there are one or more processes running, and off when there are no processes running?
I imagine it would be possible to maintain a count of the number of processes running. hide_busy_icon()
only hides the icon if the process count is 0. That seems kinda prone to failure. Perhaps there's a better/simpler way that I'm not seeing.
Thanks for your ideas and suggestions!
Edit: After working with the solution in the marked answer for a while, I'm happy to say it works very well. The only issue I've run into are cases where my own scripts call functions of scripts I don't control. Unless those functions allow callbacks to be supplied there's no way to update the process-count when they begin and end.
An example of where this can occurs is adding a set of markers to a Google map. Once my script calls the Google maps function the busy icon disappears, while the markers are still being loaded.
I'm not sure of a good way to handle this.