How can i stop ajax request (don't wait untill response come)?

后端 未结 5 1005
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-10 18:46

If I use Ajax to send request and this request take long time ..... if I want to send anther request what should I do?

the current behaviour the second request

5条回答
  •  忘掉有多难
    2021-02-10 18:51

    Browser allows you to handle only limited amount of requests to same host at time (2 or 3 as I remember, depending on browser).

    Workaround on requests count is to make fake domains - like img1.domain.com, img2.domain.com, etc. leading to the same host and randomly use them in requests. Then you can just make requests you need. Domains count should be chosen depending on requests quantity in order to keep in bounds - 2 requests per domain. Otherwise 3rd request will wait until one of active finishes.

    It allows you to receive responses from all your requests. For example, Google uses it to make images load faster.

    EDIT:

    Example: you have http://yourhost.com/ and alias http://alias.yourhost.com which points to the same place. Then:

    $.ajax
    ({
        type: "GET",
        url: 'http://yourhost.com/somescript.php',
        success: function (html)
        {
           // do some thing  
        }
    });
    

    and then

    $.ajax
    ({
        type: "POST",
        url: 'http://alias.yourhost.com/somescript2.php',
        success: function (html)
        {
           // do some thing else  
        }
    });
    

提交回复
热议问题