Created
February 21, 2016 07:42
-
-
Save sapegin/c8e5693d20bd26d0f8e7 to your computer and use it in GitHub Desktop.
Tamia image preload es6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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