0

I want to refresh an image every 1 second. This picture is dynamically generated by my webcam, but this script sometimes display broken images when browser didnt yet load a picture before display. How to detect when picture is loaded and then change the diplay picture? I have a code like this:

    <img id="image" src="webcam.jpg">


<script>
setInterval(function() {
    var myImageElement = document.getElementById('image');
    myImageElement.src = 'webcam.jpg?rand=' + Math.random();
}, 1000);
</script>
5
  • 1
    500 is half a second, by the way Commented Jun 26, 2017 at 9:11
  • Possible duplicate of Detect image load by jQuery Commented Jun 26, 2017 at 9:22
  • @Liam he is not using jquery, why is then a possible duplicate of a jquery question? Commented Jun 26, 2017 at 9:25
  • The Op has tagged it jQuery. This typically means a jQuery solution is viable Commented Jun 26, 2017 at 9:26
  • Also a possible duplicate of Browser-independent way to detect when image has been loaded Commented Jun 26, 2017 at 9:26

3 Answers 3

4

You should wait for the image to be loaded first, before replacing it.

For this, you can use different approaches. What I usually do, is to create another image which is not visible and replace the one visible once the previous image is loaded.

Your code should look like this if you want to follow this approach:

<img id="image" src="webcam.jpg">


<script>
setInterval(function() {
    var url = 'webcam.jpg?rand=' + Math.random();
    var img = new Image();
    img.src = url;
    img.addEventListener('load', function() {
        var myImageElement = document.getElementById('image');
        myImageElement.src = url;
    });
}, 1000);
</script>

What you can also do is play with css styles to make the image hidden or visible depending on the state, but that would make your image appear and disappear which is a bit ugly...

I hope it helps :)

Sign up to request clarification or add additional context in comments.

Comments

2

I think you can use something like this:

var _img = document.getElementById('pic'),
urlToImage = 'pic.jpg',
modified = false, lastModified = 0;

setInterval(function(){


fetch(urlToImage)
    .then(function(resp){
    	var f = new File([resp],"file");
      
    	switch(true) {
        case modified === false:
          lastModified = f.lastModified;
          modified = true; break;
          
          default:
          modified = false;
          if(lastModified < f.lastModified) {
            modified = true;
          }
    
      }
      
    	return resp.blob();
    })
    .then(function(blobdata){
      switch(true) {
        case modified === true:
        var obj_url = URL.createObjectURL(blobdata);
    	  _img.src = obj_url;
      break;
      }
    	
    });

},1000);
<img src="" id="pic" alt="LOCAL pic here"/>

I think that looking at lastModified time in File properties is a good way to assume image was finished written on the server.

Hope this helps.

Comments

-2

Try using this after loading the document

$( document ).ready(function() {
    setInterval(function() {
    var myImageElement = document.getElementById('image');
    myImageElement.src = 'webcam.jpg?rand=' + Math.random();
}, 500);
});

Hope this helps

6 Comments

He is not using jquery, why adding a new dependency?
Well, one of the tags says jquery. If he's not using it, the tag should be removed.
jQuery is fine for this question, this answer though won't work. At all.
I used jquery, since he tagged this under jquery.
@Prags I can use jQuery, thats not a problem.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.