adding core.preloadImages()

This commit is contained in:
Josh de Leeuw 2013-11-11 23:09:21 -05:00
parent 1c5445d285
commit a04f33f30e

View File

@ -144,6 +144,33 @@
return exp_start_time; return exp_start_time;
}; };
// core.preloadImage will load images into the browser cache so that they appear quickly when
// used during a trial.
// images: array of paths to images
// callback_complete: a function with no arguments that calls when loading is complete
// callback_load: a function with a single argument that calls whenever an image is loaded
// argument is the number of images currently loaded.
core.preloadImages = function(images, callback_complete, callback_load){
var n_loaded = 0;
var loadfn = (typeof callback_load === 'undefined') ? function(){} : callback_load;
var finishfn = (typeof callback_complete === 'undefined') ? function(){} : callback_complete;
for(var i=0;i<images.length;i++){
var img = new Image();
img.onload = function(){
n_loaded++;
loadfn(n_loaded);
if(n_loaded == images.length){
finishfn();
}
};
img.src = images[i];
}
};
// //
// private functions // // private functions //
// //