Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to show image only when it is completely loaded?

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.

<img id="stream"
  width="1280" height="720" 
  alt="Press reload if no video displays" 
  border="0" style="cursor:crosshair; border:medium; border:thick" />

<button type="button" id="btnStartLive" onclick="onStartLiveBtnClick()">Start Live</button>

javascript code

function LoadImage()
{
  x = document.getElementById("stream");    
  x.src = "http://IP:PORT/jpg/image.jpg" + "?" + escape(new Date());
}

function onStartLiveBtnClick()
{       
  intervalID = setInterval(LoadImage, 0);
}

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

like image 656
Sarfraz Ahmed Avatar asked Jan 17 '13 06:01

Sarfraz Ahmed


People also ask

How can we detect an image been fully loaded in the browser?

The image is considered completely loaded if any of the following are true: Neither the src nor the srcset attribute is specified. The srcset attribute is absent and the src attribute, while specified, is the empty string ( "" ). The image resource has been fully fetched and has been queued for rendering/compositing.

How do you check whether an image is loaded or not?

Using attributes of <img> to check whether an image is loaded or not. The attributes we will use are: onload: The onload event is triggered when an image is loaded and is executed. onerror: The onerror event is triggered if an error occurs during the execution.

How do you load and display an image in HTML?

To display an image, use the <img> tag with the src attribute the way you'd use the href attribute in an <a> tag.

What is .complete in JS?

Understanding JavaScript: What is the Completion Record The Completion type is a Record used to explain the runtime propagation of value and control flow such as the behaviour of statements (break, continue, return and throw) that perform nonlocal transfers of control. — ECMAScript Specification.


1 Answers

Preload the image and replace the source of the <img /> after the image has finished loading.

function LoadImage() {
    var img = new Image(),
        x = document.getElementById("stream");

    img.onload = function() {
        x.src = img.src;
    };

    img.src = "http://IP:PORT/jpg/image.jpg" + "?_=" + (+new Date());
}
like image 98
Andreas Avatar answered Oct 04 '22 15:10

Andreas