Skip to content

Commit

Permalink
PreloadImages method will auto-flatten the input array
Browse files Browse the repository at this point in the history
Closes #42
  • Loading branch information
jodeleeuw committed May 13, 2014
1 parent 8267154 commit c88c078
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions jspsych.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,15 @@
// 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;

// flatten the images array
images = flatten(images);

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++){
for(var i=0;i<images.length;i++){
var img = new Image();

img.onload = function(){
Expand All @@ -177,7 +181,7 @@
};

img.src = images[i];
}
}
};

// core.turkInfo gets information relevant to mechanical turk experiments. returns an object
Expand Down Expand Up @@ -417,6 +421,19 @@
}
return r;
}

// private function to flatten nested arrays
function flatten(arr, out) {
out = (typeof out === 'undefined') ? [] : out;
for (var i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
flatten(arr[i], out);
} else {
out.push(arr[i]);
}
}
return out;
}

return core;
})();
Expand Down

0 comments on commit c88c078

Please sign in to comment.