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