Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanmarkovich/79d66c5ddaae63156871fda9308a9eee to your computer and use it in GitHub Desktop.
Save ivanmarkovich/79d66c5ddaae63156871fda9308a9eee to your computer and use it in GitHub Desktop.
preload images
//via https://stackoverflow.com/questions/8264528/image-preloader-javascript-that-supports-events/8265310#8265310
function preloadImages(srcs, imgs, callback) {
var img;
var remaining = srcs.length;
for (var i = 0; i < srcs.length; i++) {
img = new Image();
img.onload = function() {
--remaining;
if (remaining <= 0) {
callback();
}
};
img.src = srcs[i];
imgs.push(img);
}
}
// then to call it, you would use this
var imageSrcs = ["src1", "src2", "src3", "src4"];
var images = [];
preloadImages(imageSrcs, images, myFunction);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment