JSFiddle - React, Tailwind, and code Playground

by sandwichest

JavaScript

//Preload Images with Arguments and/or Arrays Function

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

function preloadImages() {
  const images = []
  let c = 0
  for (var i = 0; i < arguments.length; i++) {
    if (Array.isArray(arguments[i])) {
      for(var arr = 0; arr < arguments[i].length; arr++) {
        if(typeof arguments[i][arr] == 'string') {
            images[c] = preloadImage(arguments[i][arr])
            c++
        }
      }
    }
    else if(typeof arguments[i] == 'string') {
      images[c] = preloadImage(arguments[i])
      c++
    }
  }
  return images
}

  //-- usage --//
var arr = [
  "https://image.shutterstock.com/image-photo/word-example-written-on-magnifying-260nw-1883859943.jpg",
  "https://image.shutterstock.com/image-photo/example-word-written-on-wooden-260nw-1765482248.jpg"
]

const images = preloadImages(
  arr,
  "https://image.shutterstock.com/image-vector/example-red-square-grunge-stamp-260nw-327662909.jpg",
  "https://thumbs.dreamstime.com/b/examples-tag-banner-d-rendered-blue-isolated-white-background-112041886.jpg",
  [
    "https://thumbs.dreamstime.com/b/examples-tag-banner-d-rendered-blue-isolated-white-background-112041886.jpg", 
    "https://image.shutterstock.com/image-photo/hand-turns-cubes-changes-expression-260nw-1824717938.jpg"
  ]
)

console.dir(images)