I want to build a function that fires with every ajax request. Like showing "loading...". I am using Jquery is that possible?
-
3You should show what you have tried here - some code for example as this IS doable but StackOverflow is not a consulting firm. – Mark Schultheiss May 14 '12 at 12:39
-
3i think that my question doesn't need to show code because i m not facing a probleme in my code, besides do believe that s can be a technical question, may be you just didn't like it because it s a small question. – safiot May 14 '12 at 12:50
4 Answers
jQuery.active
contains the number of active jQuery AJAX connections. However, you'd have to constantly poll that var in order to have an event fired in the advent of a new connection.
Anyway, why would you be using this for a "loading" indicator? Just set the indicator whenever you do the request.

- 4,440
- 2
- 23
- 29
Yes:
http://api.jquery.com/ajaxStart/
http://api.jquery.com/ajaxStop/
HTML:
<div id="loadingDiv">Loading ajax stuff...</div>
jQuery:
$('#loadingDiv')
.hide()
.ajaxStart(function() {
$(this).show();
})
.ajaxStop(function() {
$(this).hide();
});

- 35,120
- 54
- 178
- 293
Yes, this is totally possible. Start with the global ajax handlers here http://api.jquery.com/category/ajax/global-ajax-event-handlers/ .ajaxStart() and .ajaxStop() events are the most likely candidate to show your status indicator.

- 3,650
- 1
- 19
- 19
As is said before, $.active contains the number of active jQuery AJAX connections. When the $.active reaches 0, all AJAX has been loaded. So, you can set a bucle and wait for all the executions. I found the solution here, "etgregor" answer