Richard Howell
Richard Howell

Reputation: 11

Javascript Image Refresh

I am using the following code, to try and refresh two webcam images on my site.

var t_webcam = 60 // interval in seconds 
      image_webcam = "cam_1.jpg" //name of the image 
      function Start_webcam() { 
      tmp_webcam = new Date(); 
      tmp_webcam = "?"+tmp_webcam.getTime() 
      document.images["frontcam"].src = image_webcam+tmp_webcam 
      setTimeout("Start_webcam()", t_webcam*1000) 
      } 
      Start_webcam();

On the site i am calling the function using the following line of code

However when i load this onto the website i get the following error and it dosen't work,

Uncaught TypeError: Cannot set property 'src' of undefined

If anyone out there has some more knowledge on Javascript and would be able to offer some help i would be greatful.

Thanks Richard

Upvotes: 1

Views: 1411

Answers (4)

verheesj
verheesj

Reputation: 1458

I assume frontcam is an ID?

Try replacing

document.images["frontcam"].src

with

document.getElementById('frontcam').src

Upvotes: 0

Alex K
Alex K

Reputation: 7217

Add an id="frontcam" to your <img> tag if it doesn't have it yet, and try this code

var t_webcam = 60; // interval in seconds 
var image_webcam = "cam_1.jpg"; //name of the image 
function Start_webcam() { 
  var tmp_webcam = new Date(); 
  tmp_webcam = "?"+tmp_webcam.getTime();
  document.getElementById("frontcam").src = image_webcam+tmp_webcam;
  setTimeout(Start_webcam, t_webcam*1000);
} 
Start_webcam();

Upvotes: 0

Reimius
Reimius

Reputation: 5712

The line:

document.images["frontcam"].src = image_webcam+tmp_webcam

is invalid, it should probably be something more like this:

 document.getElementById("frontcam").src = image_webcam+tmp_webcam;

Upvotes: 0

Matt Zeunert
Matt Zeunert

Reputation: 16561

document.images is an array, so it will only accepted numerical indexes. You are trying to access it like a dictionary, passing frontcam as a key.

You can use document.getElementById instead if your tag looks like this:

<img src="sth.jpg" id="frontcam"></img>

Upvotes: 2

Related Questions