Skip to content

Instantly share code, notes, and snippets.

@sapegin
Created February 21, 2016 07:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sapegin/c8e5693d20bd26d0f8e7 to your computer and use it in GitHub Desktop.
Save sapegin/c8e5693d20bd26d0f8e7 to your computer and use it in GitHub Desktop.
Tamia image preload es6
// Image preload
import { ensureArray } from './util';
export default function preload(images, callback) {
let done = () => {
counter--;
if (counter === 0) {
callback(errors.length ? errors : null);
}
};
let error = function() {
errors.push(this.src); // eslint-disable-line no-invalid-this
done();
};
images = ensureArray(images);
let counter = images.length;
let errors = [];
images.forEach((src) => {
let img = new Image();
img.onload = done;
img.onerror = error;
img.src = src;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment