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?
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 ...
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.
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.
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.
You want the .ajaxSend
event, which is sent for every AJAX request, not just the first outstanding one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With