Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto refresh images in HTML

I am using following code

<html>
<script>

    var newImage = new Image();

function updateImage() {
    if(newImage.complete) {
           newImage.src = document.getElementById("img").src;
           var temp = newImage.src;
           document.getElementById("img").src = newImage.src;
           newImage = new Image();
           newImage.src = temp+"?" + new Date().getTime();

}
setTimeout(updateImage, 1000);
};
</script>

<body onload="updateImage();">
<img id="img" src="http://cameraURI" style="position:absolute;top:0;left:0;height:50%;width:50%"/>
</body>
</html>

But my image is not getting refreshed. Note that for my application purpose, I cant use any url in script.

I know I need to remove newImage.src = document.getElementById("img").src; and need to place over function updateImage() in same file but if I do this, I am getting error as document.getElementById(" ").src is set to NULL and I cant use auto-refresh HTML page. So any help on this file??

like image 657
Rohit Avatar asked Jan 02 '14 06:01

Rohit


People also ask

How do I refresh an image in HTML?

To refresh the image in JavaScript, we can simply select the img element and modify its src attribute to be that of the target image, along with the timestamp parameter to ensure it does not access it from the cache. Example: HTML.

How do you automatically refresh a page in HTML?

Approach 1: One can auto refresh the webpage using the meta tag within the head element of your HTML using the http-equiv property. It is an inbuilt property with HTML 5. One can further add the time period of the refresh using the content attribute within the Meta tag.

How do I reload photos?

Just right on image and select "reload this image" or right click on page and click "reload all images" in Tab or Window.

Can you make a website refresh automatically?

It's as simple as going to your browser's app/extension store and finding one you like: Launch your browser. Go to app/extension store (Chrome Web Store, Firefox Add-Ons, Microsoft Edge Add-ons Store, etc.). Enter “auto-refresh” in the search bar.


1 Answers

try this

function refresh(node)
{
   var times = 3000; // gap in Milli Seconds;

   (function startRefresh()
   {
      var address;
      if(node.src.indexOf('?')>-1)
       address = node.src.split('?')[0];
      else 
       address = node.src;
      node.src = address+"?time="+new Date().getTime();

      setTimeout(startRefresh,times);
   })();

}

window.onload = function()
{
  var node = document.getElementById('img');
  refresh(node);
  // you can refresh as many images you want just repeat above steps
}
like image 153
Voonic Avatar answered Sep 20 '22 15:09

Voonic