How can I know that no one AJAX request is being executed right now or all AJAX requests are completed?
I need to write an if
statement. I need js native or jquery.
Like:
if(```no ajax pending```){
//do some
}
Thanks!
How can I know that no one AJAX request is being executed right now or all AJAX requests are completed?
I need to write an if
statement. I need js native or jquery.
Like:
if(```no ajax pending```){
//do some
}
Thanks!
You can set up events that track ajax requests in jquery
https://api.jquery.com/ajaxstart/
https://api.jquery.com/ajaxstop
let ajaxRunning = false ;
$( document ).ajaxStart(function() {
ajaxRunning = true;
}).ajaxStop(function() {
ajaxRunning = false;
});
if (!ajaxRunning) {
// nothin doin
}
ajaxStop
gets tripped when all requests have been completed
Make a wrapper around fetch
or $.ajax
or whichever you use that adds each request made to a Set, and check the set's size.
const requests = new Set();
const makeReq = (...args) => {
const initialRequest = fetch(...args);
const bodyRequest = request
.then(res => res.text())
.finally(() => {
requests.delete(bodyRequest);
});
requests.add(bodyRequest);
return initialRequest;
};
Then, when making a request, always use makeReq
instead of fetch
. No jQuery required.