Is it possible to tell if there are any ongoing 'GET' requests with javascript?
Is it possible to tell if there are any ongoing 'GET' requests with javascript?
I have a feeling that it is not. Basically I don't want to make a seperate request while the other "more important" requests are going as this one is fairly heavy. So I was curious if it is possible to tell if there are currently 'get' requests going a开发者_如何学Cnd if so I can tell my function to hold off for this update and do it again in 10-15secs.
Any information etc would be appreciated.
I don't think you can do this using just javascript. You can use a framework like jQuery and manage all the requests using a plugin like ajax manager.
I don't know of any built in api for it, but it's as simple as setting a global flag for it.
var get = 0;
function somethingeasy() {
get++;
$.get(somethingeasy, function() {
get--;
stuff;
});
};
function somethinghard() {
if(get)
{
setTimeout('somethinghard',1000);
return false;
}
get++;
$.get(somethinghard, function() {
get--;
stuff;
});
};
If all the AJAX calls are made with jQuery, you can use the ajaxSend()
event...
$.ajaxSend(function(e, xhr, settings) {
if(settings.method == "GET") {
alert("GET request is being made...");
}
});
Rather than looking for requests directly, you can use ready/load events to check when they are done - presumably you know which objects you want to wait for. If you want to wait for all images to load, don't let your code run until after document.load has been called.
jQuery 2+ you can use:
$(document).ajaxStart(function(){
return console.log("an ajax event started");
});
$(document).ajaxStop(function(){
return console.log("all ajax events stopped");
});
精彩评论