To avoid potential delays, it is advisable to preload images instead of setting the image source inside a loop. This precaution will prevent disruptions that may occur due to the time taken for loading and decoding images, especially when the process exceeds 50 ms (whether cached or not). Random appearance of issues, particularly on slower devices or connections, can be resolved by preloading images and inserting them into the designated container afterwards.
There are two common methods for preloading images - concealing them in DOM and triggering loading using window.onload, or employing JavaScript with an array and load counter. Below is an example of a loader:
var images = [],
count = nextImage5.length,
len = count,
i = 0;
for(; i < len; i++) {
var img = new Image;
images.push(img);
img.onload = loader;
img.src = nextImage5[i];
}
function loader() {
count--;
if (count === 0) {
... initiate animation at this point...
}
}
In the animation loop, you can use a similar approach like below (adapted jQuery code):
$('#myImageHolder8').html('');
$('#myImageHolder8').append(images[u]);