Image preloader and limiting concurrent downloads
<p>I have looked at a million image preloading scripts and tried to write a new one combining the best of all of them, it works brilliantly but I need one change. Currently it downloads as many images concurrently as possible. For my purposes I need it to only download a defined amount of images at a time. This causes other downloads happening outside the script to have to wait or slow down noticeably. Any ideas of how I make it enforce 2 simultaneous downloads at the a time (and waiting to start the next one) would be appreciated.</p>

<pre><code>function loadImages(images){

  var images = (typeof images != "object") ? [images] : images;

  var imageCounter = 0;

  var postAction = function(){}

  function imageLoadPost(){

    imageCounter++;

    if(imageCounter == images.length){

      //We've run through all the images and now what?

      console.log("Images have been preloaded");

      postAction(imageLoadQueue);

    }

  }

  for (var i = 0; i &lt; images.length; i++){

    imageFileName = images[i];

    imageLoadQueue[i] = new Image();

    imageLoadQueue[i].onload=function(){

      imageLoadPost();

    }

    imageLoadQueue[i].onerror = function(){

      console.log("Error preloading image: " + imageFileName);

      imageLoadPost();

    }

    imageLoadQueue[i].src = images[i];

  }

  return { //return an object with done() method

        done : function(f){

            postAction=f || postAction;

        }

 }

}
</code></pre>
