Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

213
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda