Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda