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

207
Views
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 answers
Answer question

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 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!