Multiple AJAX requests are not calling onSuccess until all requests are done.

Multiple AJAX requests are not calling onSuccess until all requests are done.

Ok, so I'm working on a little photo gallery that will load a set of images using AJAX.  Basically it will load a 'loading.gif' placeholder image, make an AJAX request to generate the thumbnail image.  PHP page generates the thumbnail and sends a confirmation that the image has been generated and the path of it.  My onSuccess should then replace the loading.gif with the actual thumbnail.

The problem: This all works just fine, except it won't start putting the thumbnail images in until after ALL the unfinished requests are done.  So instead of having the images appear 1 or 2 seconds apart, on a page with 20 thumbnails to generate I wait 20 seconds then while the AJAX requests are going, then begins to display the thumbnails, even though some have been ready for a while.

Here is my relevant code:

  1. function getImageList() {
  2. //Get an XML list of all the images to be displayed.
  3. $.ajax({
  4. url:"gallery.php",
  5. type: "POST",
  6. data:{mode: "getImageList"},   
  7. success: function(responseText){  
  8. $(responseText).find('galImg').each(function(){

  9. //create the image divs and images, set properties and get values.
  10. var imageBox = document.createElement('div');
  11. var imageElement = document.createElement('img');
  12. imageBox.setAttribute('class','imageBox');
  13. imgThumbPath = $(this).find('img_thumb').text();
  14. imgThumbReady = $(this).find('img_thumb_ready').text();
  15. imgPath = $(this).find('img_path').text();
  16. imageElement.setAttribute('id',imgPath);
  17. imageBox.appendChild(imageElement);
  18. $('#galleryContainer').append(imageBox);  
  19. //now load the src of the image
  20. if (imgThumbReady=='no') {
  21. imageElement.setAttribute('src',loadingImage);
  22. $.ajax( {
  23. url: "gallery.php", 
  24. type: "POST",
  25. data: {mode: "generateThumbnail",imgPath:imgPath},   
  26. success: function(response){  
  27. if ($(response).find('message').text() == 'Sucess') {
  28. imageElement.setAttribute('src',$(response).find('galImg').find('img_thumb').text());
  29. } else {
  30. alert('Problem Loading Image - '+$(response).find('message').text());
  31. }
  32. },
  33. datatype: "html"
  34. } );
  35. } else {
  36. imageElement.setAttribute('src',imgThumbPath);
  37. }
  38. }); 
  39. },  
  40. datatype:"html"  
  41. });  

  42. }