Eugeniu Torica
Eugeniu Torica

Reputation: 7574

Is there a way to add additional behavior to jquery's $.ajax function

The problem is the following. We have lots of ajax call via $.ajax function. Now I would like to add some loader indicator that will show up when ajax call started and disappear when ajax call finishes. Is there a way to extend jquery's $.ajax function with this kind of behavior.

Of course it is always possible to search for all $.ajax in code and add necessary behavior but I feel somehow lazy to do this.

Upvotes: 2

Views: 106

Answers (2)

rogeriopvl
rogeriopvl

Reputation: 54104

You have jQuery functions for that. Take this example:

<div id="loading">Loading...</div>

Now the jQuery code:

$(document).ready(function() {

  $().ajaxStart(function() { $('#loading').show(); });
  $().ajaxStop(function() { $('#loading').hide(); });

});

ajaxStart detects that an ajax call is being made and executes a function. ajaxStop will detect that the ajax call has ended and will execute a function. This works with any ajax call in your code.

Upvotes: 3

Vinay Sajip
Vinay Sajip

Reputation: 99415

You use ajaxStart/ajaxStop. may find this article to be of help.

Upvotes: 1

Related Questions