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

210
Visualizações
Why i'm getting a infinite loop inside this function?
function renderCarousel(value) {
    let carouselSize = [];
    let tempImg = [];
    for(let i = 0; i<value; i++){   

        tempImg[i] = new Image();
        tempImg[i].onload = function(){
            saveImages();
        }
        function tryLoadImage(){  
            return (tempImg[i].src = 'images/' + i + '.jpg');
        }
        function saveImages(){
            console.log(tryLoadImage(i) + i)
            return(carouselSize[i]=tryLoadImage());
        }
        console.log(tryLoadImage(i) + '2');

}
}
  
renderCarousel(10);

Why i'm getting infinite loop inside saveImages funtion? I'm developing a image carousel so i'm creating x amount of image objects, but something goes wrong.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It's not technically an infinite loop, but it's going to trigger your onload eventually each time you assign a string to the src property, regardless of whether it's already been set before.

Change tryLoadImage(i) to tempImg[i].src inside the definition of saveImages() to avoid infinitely refetching.

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