in datawig-js/static/jspsych-6.1.0/jspsych.js [2504:2573]
module.autoPreload = function(timeline, callback, images, audio, video, progress_bar) {
// list of items to preload
images = images || [];
audio = audio || [];
video = video || [];
// construct list
for (var i = 0; i < preloads.length; i++) {
var type = preloads[i].plugin;
var param = preloads[i].parameter;
var media = preloads[i].media_type;
var func = preloads[i].conditional_function;
var trials = timeline.trialsOfType(type);
for (var j = 0; j < trials.length; j++) {
if (trials[j][param] && typeof trials[j][param] !== 'function') {
if ( !func || func(trials[j]) ){
if (media === 'image') {
images = images.concat(jsPsych.utils.flatten([trials[j][param]]));
} else if (media === 'audio') {
audio = audio.concat(jsPsych.utils.flatten([trials[j][param]]));
}
else if (media === 'video') {
video = video.concat(jsPsych.utils.flatten([trials[j][param]]));
}
}
}
}
}
images = jsPsych.utils.unique(jsPsych.utils.flatten(images));
audio = jsPsych.utils.unique(jsPsych.utils.flatten(audio));
video = jsPsych.utils.unique(jsPsych.utils.flatten(video));
// remove any nulls false values
images = images.filter(function(x) { return x != false && x != null})
audio = audio.filter(function(x) { return x != false && x != null})
video = video.filter(function(x) { return x != false && x != null})
var total_n = images.length + audio.length + video.length;
var loaded = 0;
if(progress_bar){
var pb_html = "<div id='jspsych-loading-progress-bar-container' style='height: 10px; width: 300px; background-color: #ddd;'>";
pb_html += "<div id='jspsych-loading-progress-bar' style='height: 10px; width: 0%; background-color: #777;'></div>";
pb_html += "</div>";
jsPsych.getDisplayElement().innerHTML = pb_html;
}
function update_loading_progress_bar(){
loaded++;
if(progress_bar){
var percent_loaded = (loaded/total_n)*100;
jsPsych.getDisplayElement().querySelector('#jspsych-loading-progress-bar').style.width = percent_loaded+"%";
}
}
// do the preloading
// first the images, then when the images are complete
// wait for the audio files to finish
module.preloadImages(images, function() {
module.preloadAudioFiles(audio, function() {
module.preloadVideo(video, function() {
callback();
}, update_loading_progress_bar);
}, update_loading_progress_bar);
}, update_loading_progress_bar);
}