1. (function($){
  2. //cache needed for overagressive garbage collectors
  3. var cache = [];
  4. $.loadImages = function(images, callback) {
  5. // if our first argument is an string, we convert it to an array
  6. if (typeof images == "string") {
  7. images = [images];
  8. }
  9. var imagesLength = images.length;
  10. var loadedCounter = 0;
  11. // Loop through our array
  12. for (var i = 0; i < imagesLength; i++) {
  13. // Create a DOM element for our image
  14. var cacheImage = document.createElement('img');
  15. // Define onload event callback
  16. cacheImage.onload = function() {
  17. loadedCounter++;
  18. if (loadedCounter == imagesLength) {
  19. if (typeof callback == "function") {
  20. callback.call();
  21. }
  22. }
  23. }
  24. // Add our image to the DOM
  25. cacheImage.src = images[i];
  26. cache.push(cacheImage);
  27. }
  28. }
  29. })(jQuery)