4

I want to build a function that fires with every ajax request. Like showing "loading...". I am using Jquery is that possible?

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
safiot
  • 336
  • 1
  • 4
  • 10
  • 3
    You 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
  • 3
    i 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 Answers4

9

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.

ubik
  • 4,440
  • 2
  • 23
  • 29
3

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();
});
Johan
  • 35,120
  • 54
  • 178
  • 293
3

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.

Chad Ruppert
  • 3,650
  • 1
  • 19
  • 19
1

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

How do I know if jQuery has an Ajax request pending?

Community
  • 1
  • 1
Mikel
  • 141
  • 11