ajaxStop event


.on( "ajaxStop", handler )Returns: jQuery

Description: Register a handler to be called when all Ajax requests have completed. This is an Ajax Event.

This page describes the ajaxStop event. For the deprecated .ajaxStop() method, see .ajaxStop().

Whenever an Ajax request completes, jQuery checks whether there are any other outstanding Ajax requests. If none remain, jQuery triggers the ajaxStop event. Any and all registered ajaxStop handlers are executed at this time. The ajaxStop event is also triggered if the last outstanding Ajax request is cancelled by returning false within the beforeSend callback function.

To observe this method in action, set up a basic Ajax load request:

1
2
3
<div class="trigger">Trigger</div>
<div class="result"></div>
<div class="log"></div>

Attach the event handler to the document:

1
2
3
$( document ).on( "ajaxStop", function() {
$( ".log" ).text( "Triggered ajaxStop handler." );
} );

Now, make an Ajax request using any jQuery method:

1
2
3
$( ".trigger" ).on( "click", function() {
$( ".result" ).load( "ajax/test.html" );
} );

When the user clicks the element with class trigger and the Ajax request completes, the log message is displayed.

Additional Notes:

  • As of jQuery 1.9, all the handlers for the jQuery global Ajax events, including those added with .on( "ajaxStop", ... ), must be attached to document.
  • If $.ajax() or $.ajaxSetup() is called with the global option set to false, the ajaxStop event will not fire.

Example:

Hide a loading message after all the Ajax requests have stopped.

1
2
3
$( document ).on( "ajaxStop", function() {
$( "#loading" ).hide();
} );