Created
October 23, 2015 05:41
-
-
Save paveltretyakovru/c8d8deb7b29585d3a622 to your computer and use it in GitHub Desktop.
Javascript images preloader
This file contains hidden or 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
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