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

The problem is as follows. We have a lot of ajax calls through the $ .ajax function. Now, I would like to add a loading indicator that will show when the ajax call is started and disappear when the ajax call finishes. Is there a way to extend jquery $ .ajax function with this behavior.

Of course, you can always search for all the $ .ajax in the code and add the necessary behavior, but I'm somehow lazy to do it.

+2


source to share


2 answers


You have jQuery functions. 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 in progress and executes the function. ajaxStop will detect that the ajax call has ended and will execute the function. This works with any ajax call in your code.

+3


source


You are using ajaxStart

/ ajaxStop

. can find this article to help.



+1


source







All Articles