Last active
January 12, 2021 16:07
-
-
Save davidbwaters/e6b74ff9cd14630266bc34a9016be179 to your computer and use it in GitHub Desktop.
image preload
This file contains 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
/* | |
* 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