1

There is a page on which a lot of requests ajax. When performing queries I want to display a block with the text "Loading".

$(document).ajaxStart(function() { $( "#loader" ).show(); }); $(document).ajaxStop(function(){ $( "#loader" ).hide(); }); 

But it works only on the first request.

4
  • I don't understand this question. Does "lot of requests ajax" mean simultaneous requests? Can you add a bit more context? Commented Sep 8, 2014 at 8:36
  • The description of ajaxStart from the jquery website states "Register a handler to be called when the first Ajax request begins." - That is, it's supposed to be called when the first Ajax request begins, as you've noted. Commented Sep 8, 2014 at 8:36
  • 1
    Does your AJAX request complete before you fire the second one? The documentation clearly mentions that .ajaxStop fires when there are no pending AJAX requests. Commented Sep 8, 2014 at 8:48
  • Why you are not using normal ajax request. Commented Sep 8, 2014 at 9:09

3 Answers 3

2

That's how ajaxStart works, from the docs:

Description: Register a handler to be called when the first Ajax request begins

I think you should show and hide the loader for each ajax call manually, call $( "#loader" ).show(); before the ajax call and then use the complete option of the ajax to hide it , like this:

$( "#loader" ).show(); $.ajax({ type: "POST", url: url, data: data, success: function(msg) { //stuffs }, complete: function() { $( "#loader" ).hide(); } }); 

Edited:

If you don't want to edit your ajax requests, maybe you could try ajaxSend() and ajaxComplete(), which are supposed to be executed for all ajax calls, instead of ajaxStart() and ajaxStop():

 $(document).ajaxSend(function() { $( "#loader" ).show(); }); $(document).ajaxComplete(function(){ $( "#loader" ).hide(); }); 
Sign up to request clarification or add additional context in comments.

4 Comments

I have a lot of elements and ajax-functions on the page. Wanted to do faster.
@ДмитрийГалушко Edited answer with extra info to do it faster, maybe it could work.
I did not want to edit each function. I was looking for a way to make it faster. Process all requests on a page in one function.
@ДмитрийГалушко With ajaxSend and ajaxComplete (the edited part of my answer) you don't have to edit each function, just change ajaxStart and ajaxStop with that functions (without changing anything more).
1

As Ruben suggested AjaxSend and AjaxComplete is the way to go.

But another problem is, since the AjaxComplete is called after every ajax request is completed, it hides the loading div just after first ajax request. But the desired behavior should be that the loading div hides after all the ajax requests are completed.

How can we achieve that you ask? Simple! In the AjaxComplete check if there are no pending ajax requests before hiding the div.

 $(document).ajaxSend(function() { $( "#loader" ).show(); }); $(document).ajaxComplete(function(){ if ($.active === 1) { $( "#loader" ).hide(); } }); 

Comments

0

Use .ajaxComplete() instead of .ajaxStop()

.ajaxComplete() - runs for every request that completes, use this when you want to do something with each request/result. Note that this doesn't replace the success handler, since the parsed data is not one of the arguments (and it runs even when there's an error) - you may want .ajaxSuccess() in some per-request situations instead.

.ajaxStop() - runs when every batch of requests completes, usually you'd use this in combination with .ajaxStart() for things like showing/hiding a "Loading..." indicator of some sort - or to do something else once a batch of AJAX requests finishes, like a master last step.

$(document).ajaxStart(function() { $( "#loader" ).show(); }); $(document).ajaxComplete(function(){ $( "#loader" ).hide(); }); 


UPDATE:-

DEMO

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.