Created
July 14, 2019 09:40
-
-
Save ivanmarkovich/79d66c5ddaae63156871fda9308a9eee to your computer and use it in GitHub Desktop.
preload images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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