I have a collection of images laid out as shown below
image1.png
image2.png
image3.png
image4.png
…
…
image20.png
secondImg1.png
secondImg2.png
secondImg3.png
secondImg4.png
secondImg5.png
…….
……..
secondImg18.png
My goal is to dynamically load one image at a time.
This is my JavaScript code
var index = 1;
var timer = setInterval(function(){playAnimation()},200);
function playAnimation(){
$('#holder').attr('src','image/image' + index + 'png')
index++
}
While my code provides a basic outline of how it functions, I am unsure about how to limit the index values to 20 and 18 in order to stop the animation. (Although using an if
statement to check for 20 or 18 could work, I believe there might be a more efficient solution)
Essentially, I would like to detect if my application attempts to load image21.png and secondImg19.png
and halt the process as these files do not exist or may be corrupted. Is there a way to achieve this? Your assistance is greatly appreciated!