Skip to content

Instantly share code, notes, and snippets.

@rezoner
Last active December 10, 2015 18:29
Show Gist options
  • Save rezoner/4474989 to your computer and use it in GitHub Desktop.
Save rezoner/4474989 to your computer and use it in GitHub Desktop.
Super simple javascript image loader.
/* usage:
var image = new Image;
simploader(image); // also accept array of images
image.src = "something.png";
simploader(function() {
// some action on image ready
});
*/
simploader = (function() {
var count = 0;
var index = 0;
var callbacks = [];
var simploader = function() {
if(arguments[0] instanceof Array) {
for(var i = 0; i < arguments[0].length; i++) {
add(arguments[0][i]);
}
} else if(arguments[0] instanceof Image) {
add(arguments[0]);
} else {
callbacks.push(arguments[0]);
}
}
function add(image) {
count++;
image.onload = function() {
count--;
if(!count) {
for(var i = 0; i < callbacks.length; i++) {
callbacks[i]();
}
callbacks = [];
}
}
}
return simploader;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment