I have an img tag on my web page. I give it the url for an IP camera from where it get images and display them. I want to show image when it is completely loaded. so that I can avoid flickering. I do the following.
JavaScript
x
7
1
<img id="stream"
2
width="1280" height="720"
3
alt="Press reload if no video displays"
4
border="0" style="cursor:crosshair; border:medium; border:thick" />
5
6
<button type="button" id="btnStartLive" onclick="onStartLiveBtnClick()">Start Live</button>
7
javascript code
JavaScript
1
11
11
1
function LoadImage()
2
{
3
x = document.getElementById("stream");
4
x.src = "http://IP:PORT/jpg/image.jpg" + "?" + escape(new Date());
5
}
6
7
function onStartLiveBtnClick()
8
{
9
intervalID = setInterval(LoadImage, 0);
10
}
11
in this code. when image is large. it takes some time to load. in the mean time it start showing the part of image loaded. I want to display full image and skip the loading part Thanks
Advertisement
Answer
Preload the image and replace the source of the <img />
after the image has finished loading.
JavaScript
1
11
11
1
function LoadImage() {
2
var img = new Image(),
3
x = document.getElementById("stream");
4
5
img.onload = function() {
6
x.src = img.src;
7
};
8
9
img.src = "http://IP:PORT/jpg/image.jpg" + "?_=" + (+new Date());
10
}
11