I have a folder with about 100 images, the total size of which is about 6MB. I have an application where, after a while, images from this folder begin appearing randomly as background. As a result, I noticed that – especially with slower connections – there is a lag when the image is chosen for the first time.
To avoid this issue, I decided to preload the images this way:
var pics= ["1.jpg", "2.jp3" … … "100.jpg"];
for (var j=0;j<pics.length;j++) {
preloadImage(pics[j]);
}
function preloadImage(url) {
var img=new Image();
img.src=url;
}
The code works as intended. I'm using this very early in the application, and as a result by the time an image is called randomly, it's already cached and appears instantly.
If we assumed the size of this folder was larger (by an arbitrary number; say, if there were 1000 images and 60MB in total), are there any critical problems I should be aware of? By "critical" here I refer to e.g. the browser crashing or slowing down (especially with low-end devices, such as mobiles with a small amount of memory). Or is the only issue that the browser might not have time to finish preloading by the time the image is requested?
Note: I also found this question, but it doesn't directly address what I'm asking.