I am developing a flash card web page and I want to know when preloaded images have loaded or experienced an error. Once I have the status for each image I will take some action.
I am basing my code on the stackoverflow post Wait for image to be loaded before going on
function loadSprite(src) {
var deferred = $.Deferred();
var sprite = new Image();
sprite.onload = function() {
deferred.resolve();
};
sprite.src = src;
return deferred.promise();
}
and the function loading the sprites
var loaders = [];
loaders.push(loadSprite('1.png'));
loaders.push(loadSprite('2.png'));
loaders.push(loadSprite('3.png'));
$.when.apply(null, loaders).done(function() {
// callback when everything was loaded
});
I have modified the loadSprite resolve to return a status result:
deferred.resolve("STATUS RESULT");
and the when
$.when.apply(null, loaders).done(function("STATUS RESULT") {
// callback when everything was loaded
//Do something with "STATUS RESULT"
});
The problem is that I can not separate out the "STATUS RESULT" for each call of loaders.push(loadSprite(file to load));
That is I only get one variable back not one for each call of loadSprite
I did not expect my above code to work, it is more of a demonstration to myself that I can pass something back.
I am unable to work this out, all assistance gratefully accepted. Thank you,