Here, we will use the event listener to wait until all images are loaded.
How it works
- Load all the images in a variable from the document. (You can also use the querySelector to get the list of the images from the particular div)
- Loop through these images.
- Add a listener for the
loadevent on each of these images to run the
incrementImgCounterwill increment the counter
- If the counter has reached the length of images, that means they’re all loaded
Note: You can also handle the
errorevent listener for broken images.
I hope you find this article helpful.
Thank you for reading. Happy Coding..!!