You'll need a loop. You could make a count of the number of images, then decrement each time an image loads. Inside the callback, call the final function once the count gets to 0.
const images = document.getElementsByClassName("test");
let imagesLeft = images.length;
for (const image of images) {
image.addEventListener('load', () => {
imagesLeft--;
if (imagesLeft === 0) executeStuffHere();
});
}
Note that .addEventListener("onload"
will not work. You must use load
.
You could also add error listeners if you want executeStuffHere
to run once everything is finished, even if some errored.
If some images might already be loaded, check if they're .complete
first before adding the listener (and if they are, decrement).