I have something like this: loop through the ids and for each one, make an ajax request (async:true
) to the server (same domain) and append received data to a DOM element. Not a hard task to accomplish, it works. Example code:
$.each(ids, function (index, id) {
$.ajax({
type: 'POST',
url: "http://localhost/example/"+id,
success: function (data) {
$('#content').append(data);
}
});
});
My problem is: when I have to many IDs to loop through (1000 for example), it sends a lot of ajax, and the browser "freeze", when I click on a link, it waits all ajax requests to finish, then is opens the clicked link.
Timeout isn't an option, because I need to show as many received data as possible. If it takes 1 second to finish or if it takes 100 seconds, no problem with this, because the user will see the others requests that has finished
So, what's the best way to handle this ? Stop the ajax requests when a link is clicked like this ? Or there's a better way to do this