Skip to content

Instantly share code, notes, and snippets.

@davidbwaters
Last active January 12, 2021 16:07
Show Gist options
  • Save davidbwaters/e6b74ff9cd14630266bc34a9016be179 to your computer and use it in GitHub Desktop.
Save davidbwaters/e6b74ff9cd14630266bc34a9016be179 to your computer and use it in GitHub Desktop.
image preload
/*
* Scripts - Utilities - Preload Images
*/
export function imagesPreload(imageUrls) {
let images = []
let count = 0
imageUrls.forEach(url => {
images[count] = new Image()
images[count].src = url
count++
})
return images
}
export function imagesPreloadedCheck(
imageEls, debug
) {
let loaded = 0
imagesEls.forEach(image => {
console.log(imageEls)
if (image.complete) {
loaded++
if (debug) {
console.log('debug')
console.log(
'Loaded ' +
loaded +
' of ' +
imageEls.length +
' Images'
)
}
}
})
if (loaded === imagesEls.length) {
console.log(
'Loaded ' +
loaded +
' Images'
)
return true
}
}
export function imagesPreloadedCheckWait(
imageEls, debug
) {
let loading = imagesPreloadedCheck(imageEls, debug)
if (loading) {
return true
}
else {
setTimeout(() => {
return imagesPreloadedCheckWait(
loading,
debug
)
}, 200)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment