Skip to content

Instantly share code, notes, and snippets.

@niklasp
Forked from akella/preload_array.js
Created November 26, 2020 20:40
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 niklasp/e5813bc9cee0895d946b2c7b644b02b8 to your computer and use it in GitHub Desktop.
Save niklasp/e5813bc9cee0895d946b2c7b644b02b8 to your computer and use it in GitHub Desktop.
Preload array of images with callback
function loadImages(paths,whenLoaded){
var imgs=[];
paths.forEach(function(path){
var img = new Image;
img.onload = function(){
imgs.push(img);
if (imgs.length==paths.length) whenLoaded(imgs);
}
img.src = path;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment