Image preloading

Vanilla JavaScript:

(new Image()).src = url;

Function:

const listOfURLs = [url1, url2, ...];

function preloadImage(url) {
   (new Image()).src = url;
}
for (let i = 0; i < listOfURLs.length; i++) {
   preloadImage(listOfURLs[i]);
}

Just another code snippet I am prone to forget.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.