简体   繁体   English

防止Ajax请求超时(jquery)

[英]Prevent ajax requests from timing out (jquery)

I have about 100 ajax requests that I fire at the same time, I thought browsers only allowed a few requests simultaneously, so the rest would be added to a queue. 我有大约100个同时发出的ajax请求,我认为浏览器只允许同时允许几个请求,因此其余的将添加到队列中。

The problem however is that jquery/javascript seems to use the timeout value from the time the requests were created via jquery, not from the time the requests were actually executed by the browser. 但是,问题是jquery / javascript似乎从通过jquery创建请求开始就使用了超时值,而不是从浏览器实际执行请求开始就使用了超时值。 So I get a bunch of timeouts. 这样我就会超时。 Is it possible to have the timeout start counting from the time the request is actually going to the URI location, instead of the time it is added by jquery? 是否有可能使超时从请求实际到达URI的时间开始计数,而不是从jquery添加的时间开始计数?

You may use the timeout settings for ajax request. 您可以将timeout设置用于ajax请求。 You may find the jQuery documentation for the same at : http://api.jquery.com/jQuery.ajax/ 您可以在以下位置找到相同的jQuery文档: http : //api.jquery.com/jQuery.ajax/

However the timeout period starts at the point the $.ajax call is made; 但是,超时时间始于进行$ .ajax调用的时间点; if several other requests are in progress and the browser has no connections available, it is possible for a request to time out before it can be sent. 如果其他几个请求正在进行中,并且浏览器没有可用的连接,则可能有一个请求超时,然后才能发送该请求。 Therefore you should set some very large value for the timeout if you wish to follow this approach. 因此,如果您希望采用这种方法,则应为超时设置一些非常大的值。

A better approach would be to have a local proxy which entertains all the AJAX calls and fires them in a group of 5-10 and then when all these have finished successfully then it fires the next 5-10 requests. 更好的方法是拥有一个本地代理,该代理可以处理所有AJAX调用,并以5-10个为一组触发它们,然后,当所有这些调用成功完成后,就会触发下一个5-10个请求。

Here is a kind of queue system. 这是一种队列系统。 Starts by calling the ajax function N times, and then after each success, calls ajax function again. N次调用ajax函数开始,然后每次成功之后,再次调用ajax函数。 There is also a check in the success callback to see if all the assets have been loaded... 在成功回调中也有一个检查,以查看是否所有资产都已加载...

demo fiddle 演示小提琴

$(document).ready(function(e) {
    $("form[ajax=true]").submit(function(e) {

        e.preventDefault();

        var form_url = $(this).attr("action");
        var form_method = $(this).attr("method").toUpperCase();

        $("#loadingimg").show();

        var started = 1, done = 0;
        function ajax(){
                $.ajax({
                    url: form_url, 
                    type: form_method,      
                    data: "html=started "+(started++),
                    cache: false,
                    success: function(returnhtml){
                        done++;
                        $("#result").html(returnhtml);
                        $("#loadingimg").hide();
                        if(started <= 100){
                            ajax();
                        } else if (done == 100) {
                            alert("all done!");
                        }

                    }           
                });    
        }
        // how many concurrent calls?
        for(i=0;i<10;i++){
            ajax();
        }
    });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM