how to show image only when it is completely loaded?

后端 未结 2 1920
暗喜
暗喜 2020-12-06 20:09

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

2条回答
  •  遥遥无期
    2020-12-06 20:44

    You can use the complete property to check if the image has finished loading. However, I think there are other issues with your code, mainly you are repeatedly loading the same image. Instead, you should load it only once and then check the complete property in an interval.

    Something like this should work:

    function LoadImage()
    {
      x = document.getElementById("stream");    
      x.src = "http://IP:PORT/jpg/image.jpg" + "?" + escape(new Date());
      x.style.visibility = 'hidden';
    }
    
    function CheckIsLoaded() {
      x = document.getElementById("stream");    
      if (x.complete) x.style.visibility = 'visible';
    }
    
    function onStartLiveBtnClick()
    {       
      LoadImage();
      intervalID = setInterval(CheckIsLoaded, 0);
    }
    

提交回复
热议问题