admin管理员组

文章数量:1332383

I'm trying to make "many" xhr request but it seems that every time it waits for the answer before making another request. It's like XHR makes a queue and always wait for the previous request to finish.

What can I do to run more simultaneous xhr request?

$('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();
    });

I'm trying to make "many" xhr request but it seems that every time it waits for the answer before making another request. It's like XHR makes a queue and always wait for the previous request to finish.

What can I do to run more simultaneous xhr request?

$('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();
    });
Share Improve this question edited Dec 31, 2015 at 12:16 NIMISHAN 1,2654 gold badges21 silver badges31 bronze badges asked Dec 21, 2015 at 23:14 Gregory WullimannGregory Wullimann 5676 silver badges24 bronze badges 6
  • have you tried using web worker? – prieston Commented Dec 29, 2015 at 8:25
  • Yes and the same thing happens..Both worker are called together but the second XHR wait for the first XHR to finish – Gregory Wullimann Commented Dec 29, 2015 at 17:22
  • How about opening new browser windows, make the requset, use post message to send the response and then close the windows? Just thoughts that may help.. not very familiar with the subject. – prieston Commented Dec 29, 2015 at 18:24
  • Also second thought, use more variables or an array or object to store xhr.. the variable may be still in use untill the server respondes. – prieston Commented Dec 29, 2015 at 18:30
  • have you tried it with jquery deferred?just a thought. – prajitgandhi Commented Dec 30, 2015 at 11:23
 |  Show 1 more ment

2 Answers 2

Reset to default 11

Not a lot.

Browsers enforce a limit on:

  • The number of parallel HTTP requests to a given origin
  • The number of parallel HTTP requests in total (this is a larger limit)

If you split your requests between more originals you might find your constraint raised from the first to the second of the above.

Try to find something about http2, there is some info. Http2 protocol have better supporting of parallel requests.

本文标签: javascriptxmlhttprequest simultaneous request not workingStack Overflow