I am making a java servlet page that checks a list of domain names and it checking each name through a jquery ajax request. It works fine except the results are being appended to the DOM out of order. What is the best method to process requests in the requested order but also be asynchronously like a long polling effect. I am new with javascript.
This is the code I am using for the requests:
$.ajax({
async:true,
type:'GET',
url:servlet,
success:function(data){
$('p').append(data+"<br>");
},
});
I was thinking of sending a sequence number to the java servlet which would return it through JSON but I was wondering if there was a simpler way.
What I would do is create the containers for the response ahead of time and then bind the callback around it (not actually binding as in the bind() function).
For example, assuming you have a container something like
<div id="container"></div>, you can do the following:It’s a rather bad example in terms of flexibility, but it should illustrate the point. It adds a div to the container before the request is made and then uses that div to push the response into. That means that the divs will be appended in the order the requests are made. You could extend this idea to use styling to make it look like the divs aren’t there until they’re populated, or you could have a “Loading” message in them.
(Also, the data passing is rather bad with the
somethingparameter, but hopefully you get the point.)