mirror of
https://github.com/jspsych/jsPsych.git
synced 2025-05-10 19:20:55 +00:00
adding core.preloadImages()
This commit is contained in:
parent
1c5445d285
commit
a04f33f30e
27
jspsych.js
27
jspsych.js
@ -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 //
|
||||||
//
|
//
|
||||||
|
Loading…
Reference in New Issue
Block a user