我正在尝试制作"许多" xhr请求,但似乎每次在提出另一个请求之前等待答案.这就像XHR组成队列并始终等待前一个请求完成.
如何同时运行更多xhr请求?
$('body').delegate('.download','click', function(evt){
evt.preventDefault(); // Not related
var xhr = new XMLHttpRequest();
xhr.open('GET', "proxy.php?" + this.href, true);
xhr.responseType = 'blob';
xhr.onload = function() {
if (this.status == 200) {
var blob = new Blob([this.response], {type:'audio/mp4'});
console.log(blob.size);
if(blob.size > 0){
$("<a>").attr({
download: name,
href: URL.createObjectURL(blob),
target: "_blank"
})[0].click();
}
}
};
xhr.send();
});
Run Code Online (Sandbox Code Playgroud) 我有一个特定的函数,我想运行一次,并且只有在完成几个AJAX请求之后.
我目前的解决方案看起来有点像这样:
function doWork() {
//This is the function to be run once after all the requests
}
//some tracking/counting variables
var ajaxDoneCounter = 0;
var numOfAjaxRequests = 5;
var workDone = false;
function doWorkTrigger() {
ajaxDoneCounter++;
if( !workDone && ajaxDoneCounter >= numOfAjaxRequests ) {
workDone = true;
doWork();
}
}
// ...
//and a number of ajax requests (some hidden within functions, etc)
//they look something like this:
$.ajax({
url: "http://www.example.com",
dataType: "json",
success: function( data ) {
//load …Run Code Online (Sandbox Code Playgroud)