jquery ajax sends only 6 requests simultaneously

Tags: jquery ajax
By : Boris
Source: Stackoverflow.com
Question!

This is my servlet

public class TestServlet extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        LogFactory.getLog(getClass()).info(req.getParameter("number"));
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

Now I execute the following js in browser

console.clear();
for (var i = 0; i < 12; i++) {
  $.ajax('/test', {
    data: {
      number: i
    }
  });
}

and results are:

2016-09-15 19:35:26 INFO  TestServlet:17 - 0
2016-09-15 19:35:26 INFO  TestServlet:17 - 2
2016-09-15 19:35:26 INFO  TestServlet:17 - 1
2016-09-15 19:35:26 INFO  TestServlet:17 - 3
2016-09-15 19:35:26 INFO  TestServlet:17 - 4
2016-09-15 19:35:26 INFO  TestServlet:17 - 5
2016-09-15 19:35:28 INFO  TestServlet:17 - 8
2016-09-15 19:35:28 INFO  TestServlet:17 - 6
2016-09-15 19:35:28 INFO  TestServlet:17 - 7
2016-09-15 19:35:28 INFO  TestServlet:17 - 11
2016-09-15 19:35:28 INFO  TestServlet:17 - 10
2016-09-15 19:35:28 INFO  TestServlet:17 - 9

You can see that 6 requests came at 19:35:26 and other 6 at 19:35:28

I use default Tomcat and jquery settings. Why this can happen? Is there any way to configure this?

By : Boris


Answers

The browser limits it to 6 ajax requests at a time. You can trick it by separating the endpoints to different subdomains but that gets a little bit out of the question's scope. I don't think there's anything you can do in the browser to change that.



Browser has a limitation on sending ajax requests and use a pool and a queue to send them.

So yes there is no rules. If it important to save the order use async:false but it has very bad impact on user interface.



You can do it like following.

$('.medium-item-price').contents().last()[0].textContent='100';
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="medium-item-price">
  <span class="denominator">$</span>
  699.99
</div>

By : Azim


This video can help you solving your question :)
By: admin