Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
Preload images and prevent downloading same image multiple times in JavaScript

I'm building an Angular app which includes an image gallery. Basically an image and two buttons, previous image and next image. Below is some very minimal code for such a gallery (no Angular):

<html>
<style>
    img {
        display: block;
        height: 300px;
    }
</style>

<body>
    <img id="img"></img>
    <button id="prevB">Prev</button>
    <button id="nextB">Next</button>

    <script>
        var arr = [
            "https://firebasestorage.googleapis.com/v0/b/animaps-e8f7a.appspot.com/o/EugywRZ814yCMrMhopVF%2F3c.jfif?alt=media&token=d6ab88bc-5065-4dbc-ab9d-884a9af1380b",
            "https://firebasestorage.googleapis.com/v0/b/animaps-e8f7a.appspot.com/o/4KzYH8gMDjhKHFrTIXlB%2F1c.jpg?alt=media&token=9cd7bed2-c645-408c-acd5-77fdd9370389",
            "https://firebasestorage.googleapis.com/v0/b/animaps-e8f7a.appspot.com/o/ftGGK4Wj1ski8lJi1TRE%2F5a.jfif?alt=media&token=4a36d9c5-924f-4738-9851-cf057de74544",
        ]

        var index = 0;
        document.getElementById("prevB").addEventListener("click", () => {
            index--;
            document.getElementById("img").src = arr[index % arr.length];
        });
        document.getElementById("nextB").addEventListener("click", () => {
            index++;
            document.getElementById("img").src = arr[index % arr.length];
        });
        document.getElementById("img").src = arr[index];
    </script>
</body>

Question 1: Why is the browser downloading the same image multiple times? Each time I click Prev or Next the image is downloaded again, even if it was already downloaded. Is there a way to instruct the browser to cache(save) downloaded images? same images downloaded multiple times

Question 2: Is there a way (Angular specific or pure JS) to preload the entire list of images? That is, download all the images when the page is loaded, and then instantly seeing the next image when clicking Next.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Question 1: Why is the browser downloading the same image multiple times?

Most likely because you have the console open, and have disable cache selected. Either that or the backend is setting some sort of cache-control header

enter image description here

Question 2: Is there a way (Angular specific or pure JS) to preload the entire list of images?

Sure - I would probably create the img elements in JS, and then just swap out your precreated images (rather than just toggling the src on the same image), so something like:

const createImg = (src) => {
     const img = document.createElement('img');
     img.src = src;
     img.id="img";
     return img;
 }
 
 var arr = [
            "https://firebasestorage.googleapis.com/v0/b/animaps-e8f7a.appspot.com/o/EugywRZ814yCMrMhopVF%2F3c.jfif?alt=media&token=d6ab88bc-5065-4dbc-ab9d-884a9af1380b",
            "https://firebasestorage.googleapis.com/v0/b/animaps-e8f7a.appspot.com/o/4KzYH8gMDjhKHFrTIXlB%2F1c.jpg?alt=media&token=9cd7bed2-c645-408c-acd5-77fdd9370389",
            "https://firebasestorage.googleapis.com/v0/b/animaps-e8f7a.appspot.com/o/ftGGK4Wj1ski8lJi1TRE%2F5a.jfif?alt=media&token=4a36d9c5-924f-4738-9851-cf057de74544",
        ].map(createImg);

        var index = 0;
        document.getElementById("prevB").addEventListener("click", () => {
            index--;
            const img = document.getElementById("img");
            img.parentNode.replaceChild( arr[index % arr.length], img);
        });
        document.getElementById("nextB").addEventListener("click", () => {
            index++;
            const img = document.getElementById("img");
            img.parentNode.replaceChild( arr[index % arr.length], img);
        });
        img.parentNode.replaceChild( arr[index % arr.length], img);
#img {
    width: 400px;
}
<img id="img"></img>
    <button id="prevB">Prev</button>
    <button id="nextB">Next</button>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!