JSFiddle - React, Tailwind, and code Playground

by sandwichest

JavaScript

//Preload Images with Array Function

function preloadImage(url){
  const img = new Image();
  img.src = url;
  return img
}

function preloadImages(images) {
  for (var i = 0; i < images.length; i++) {
    images[i] = preloadImage(images[i])
  }
  return images
}

//-- usage --//
let arr = [
  "https://image.shutterstock.com/image-photo/word-example-written-on-magnifying-260nw-1883859943.jpg",
  "https://image.shutterstock.com/image-vector/example-red-square-grunge-stamp-260nw-327662909.jpg",
  "https://image.shutterstock.com/image-photo/example-word-written-on-wooden-260nw-1765482248.jpg"
]

const images=	preloadImages(arr)

console.dir(images)