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

210
Views
Javascript: preload next image

not very big on JS. I currently have a script I use to load/change background images every xxx seconds.

What I would like is to display an image and preload the next one so it displays seamlessly (ie: no jittering or slow loads).

Here is my current script, can this be adapted to achieve such a result?

<!-- Background Image Changer inpired by: https://stackoverflow.com/a/7265145 -->
var images = ['images/image01.jpg',
                'images/image02.jpg',
                'images/image03.jpg',
                'images/image04.jpg',
                'images/image05.jpg',
                'images/image06.jpg',
                'images/image07.jpg',
                'images/image08.jpg',
                'images/image09.jpg',
                'images/image10.jpg',
                'images/image11.jpg',
                'images/image12.jpg',
                'images/image13.jpg',
                'images/image14.jpg',
                'images/image15.jpg',
                'images/image16.jpg',];
var numSeconds = 30;
var curImage = 0;
function switchImage()
{
    curImage = (curImage + 1) % images.length
    document.body.style.backgroundImage = 'url(' + images[curImage] + ')'
}
window.setInterval(switchImage, numSeconds * 1000);

NOTES: There are 50 images in my script. I've only used fake named placeholders for clarity.

EDIT: To be clear, I only want one image displayed and the next (one) image to be preloaded. It's running on a RPi 3b so not much memory is available.

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

0

I want to add a different answer here. There are a few options you can use to improve your web page performance. Using <link> with preconnect and preload rel value can helps you to load resources before using them:

Use preconnect keyword for the rel attribute to tell the browsers that the user is likely to need resources from this origin and therefore it can improve the user experience by preemptively initiating a connection to that origin.

<link rel="preconnect" href="<your-images-base-url">

Use preload keyword for the rel attribute to declare fetch requests in the HTML's , specifying resources that your page will need very soon. This ensures they are available earlier and are less likely to block the page's render, improving performance. Taken from https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types/preload

Create preload link:

const preloadLink = document.createElement('link');
document.head.appendChild(preloadLink);
preloadLink.rel = 'preload';
preloadLink.as = 'image';

function preloadNextImage(href) {
  preloadLink.href = href;
}

function switchImage()
{
    curImage = (curImage + 1) % images.length
    document.body.style.backgroundImage = 'url(' + images[curImage] + ')';
    preloadNextImage(/* <next-image-url> */)
}
about 4 years ago · Juan Pablo Isaza Report

0

You could just load the next image when displaying the current one using the JavaScript Image object. When switchImages runs again then the image will be already in the browsers cache. Also, the cached images are stored in a new array, so the cache "generator" will be ran only once. With this snippet you will need enough delay between iterations, so the next image will have enough time to be downloaded from the sever.

var images = ['images/image01.jpg',
                'images/image02.jpg',
                'images/image03.jpg',
                'images/image04.jpg',
                'images/image05.jpg',
                'images/image06.jpg',
                'images/image07.jpg',
                'images/image08.jpg',
                'images/image09.jpg',
                'images/image10.jpg',
                'images/image11.jpg',
                'images/image12.jpg',
                'images/image13.jpg',
                'images/image14.jpg',
                'images/image15.jpg',
                'images/image16.jpg',];
var numSeconds = 2;
var curImage = 0;
var cache = [];
function switchImage()
{
    curImage = (curImage + 1) % images.length;
    document.body.style.backgroundImage = 'url(' + images[curImage] + ')';
    if(images[curImage + 1] && !cache[curImage + 1]) {
      cache[curImage + 1] = new Image();
      cache[curImage + 1].src = images[curImage + 1];
    }
}
window.setInterval(switchImage, numSeconds * 1000);

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!