Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ajaxStart() called only once but ajaxComplete() called multiple times

I would like to do something on the call of every AJAX request on my page.

I read here that

ajaxStart (Global Event)

This event is broadcast if an Ajax request is started and no other Ajax requests are currently running.

and

ajaxComplete (Global Event)

This event behaves the same as the complete event and will be triggered every time an Ajax request finishes.

This means I can only track the start of one ajax event and not each individual request?

$(document).ajaxStart(function () {
    var t = new Date(),
        h = t.getHours(),
        m = t.getMinutes(),
        s = t.getSeconds(),
        ms = t.getMilliseconds();
    console.log("Triggered ajaxStart handler   at " + h + ":" + m + ":" + s + ":" + ms);
});


$(document).ajaxComplete(function () {
    var t = new Date(),
        h = t.getHours(),
        m = t.getMinutes(),
        s = t.getSeconds(),
        ms = t.getMilliseconds();
    console.log("Triggered ajaxComplete handler at " + h + ":" + m + ":" + s + ":" + ms);
});

gives me

Triggered ajaxStart handler    at 11:14:33:409 
Triggered ajaxComplete handler at 11:14:33:480 
Triggered ajaxComplete handler at 11:14:33:489 
Triggered ajaxComplete handler at 11:14:33:491 
Triggered ajaxComplete handler at 11:14:33:492 
Triggered ajaxComplete handler at 11:14:33:535 
Triggered ajaxComplete handler at 11:14:33:539 
Triggered ajaxComplete handler at 11:14:33:567 
Triggered ajaxComplete handler at 11:14:33:569 

Is there any way to log every ajax start so I can attach an even to every single ajax event?

like image 582
1252748 Avatar asked Feb 25 '13 17:02

1252748


People also ask

What is the main difference between the ajaxStop and ajaxComplete?

This may not seem different when you're sending one ajax request at a time, but imagine that the network happened to slow down after you send request A and, when request B is sent 5 seconds later it comes back earlier than request A... then ajaxStop will only be triggered once after request A returns while ajaxComplete ...

What is ajaxComplete?

The ajaxComplete() method specifies a function to be run when an AJAX request completes. Note: As of jQuery version 1.8, this method should only be attached to document. Unlike ajaxSuccess(), functions specified with the ajaxComplete() method will run when the request is completed, even it is not successful.

What is ajaxStart?

The ajaxStart() method specifies a function to be run when an AJAX request starts. Note: As of jQuery version 1.8, this method should only be attached to document.

How do you check if all AJAX calls are completed?

jQuery ajaxStop() Method The ajaxStop() method specifies a function to run when ALL AJAX requests have completed. When an AJAX request completes, jQuery checks if there are any more AJAX requests. The function specified with the ajaxStop() method will run if no other requests are pending.


1 Answers

You want the .ajaxSend event, which is sent for every AJAX request, not just the first outstanding one.

like image 95
Alnitak Avatar answered Sep 29 '22 01:09

Alnitak