Created
July 17, 2012 02:18
-
-
Save Armen138/3126570 to your computer and use it in GitHub Desktop.
Quick+Dirty Image Preloader
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
/* quick + dirty image preloader */ | |
qdip = { | |
total: 0, | |
loaded: 0, | |
images: {}, | |
load: function(files) { | |
function loaded(file) { | |
qdip.loaded++; | |
qdip.trigger("progress", file); | |
if(qdip.loaded === qdip.total) { | |
qdip.trigger("load"); | |
} | |
} | |
for(var file in files) { | |
qdip.total++; | |
var img = new Image(); | |
(function(img, file){ | |
img.onload = function() { | |
loaded(file); | |
}; | |
img.onerror = function() { | |
//fail silently. | |
loaded(file); | |
}; | |
}(img, file)); | |
img.src = files[file]; | |
qdip.images[file] = img; | |
} | |
} | |
}; | |
LucidJS.emitter(qdip); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
added function scope to get the right names in onload/onerror