I am trying to use JavaScript to control the loading of images. The server that these images are stored on can only deal with 1 request at once, therefore, a webpage with two or more images would cause multiple requests to be sent to the server (overloading it, causing malformed images to be returned).
Below is an example of the page displaying the images...
<html>
<head>
<title>Load Images Synchronously</title>
</head>
<body>
<table align="left" border="1" cellpadding="0" cellspacing="0" style="width:500px;">
<tbody>
<tr>
<td height="50px" style="text-align: center;"><img id="img1" src="" /></td>
</tr>
<tr>
<td height="50px" style="text-align: center;"><img id="img2" src="" /></td>
</tr>
<tr>
<td height="50px" style="text-align: center;"><img id="img3" src="" /></td>
</tr>
<tr>
<td height="50px" style="text-align: center;"><img id="img4" src="" /></td>
</tr>
<tr>
<td height="50px" style="text-align: center;"><img id="img5" src="" /></td>
</tr>
</tbody>
</table>
</body>
</html>
As you can see, this contains 5 image tags, that are loaded via the script within the html (shown below). Setting the scr of the img tag, the images are displayed, however, they are still requested asynchronously.
<script>
document.getElementById("img1").scr="image1.png"
document.getElementById("img2").scr="image2.png"
document.getElementById("img3").scr="image3.png"
document.getElementById("img4").scr="image4.png"
document.getElementById("img5").scr="image5.png"
</script>
I need to know how to get these images to load, one after the other so not to overload my low powered server.
I have tried using IF statements to check the 'naturalheight' of the images (if it is 0, then the image has not been loaded) but I am pretty new to JavaScript and struggled with this.
I am looking for a simple solution (perhaps one that wraps around functions) and will check if the image is loaded before trying to load the next image.
I also require the code to check the image is loaded 100 times, with 100ms delay in between. If the image has not loaded after this time, move on to the next.
Please can some boffin give me some guidance as to how to code this?