code — webcam javascript

the code (part 1)

<script language="JavaScript" type="text/javascript">
<!--

// original code by jovino
// © 2001 jovino
// all rights reserved

// MODIFICATION 1 & 2
// the following variables are used to customize the web cam
// the camImage variable can be an absolute or local path to the file
// make sure the camImage variable is quoted
// and the refreshIntervalSeconds variable is not

var camImage = 'webcam.jpg';
var refreshIntervalSeconds = 60;

// that’s all the javascript you need to customize!!
// please do not modify any of the script below this line
// there are no user serviceable parts inside

var secondsLeft = refreshIntervalSeconds;

function startClock() {
  if (secondsLeft > 0) {
    if (secondsLeft < 10) {
      document.webCam.timer.value = '0' + secondsLeft;
    } else {
      document.webCam.timer.value = secondsLeft;
    }
    secondsLeft = secondsLeft - 1
    timerID = setTimeout('startClock()', 1000);
  } else {
  date = new Date();
  imageNumber = date.getTime();
  document.webCamImage.src = camImage + '?' + imageNumber;
  secondsLeft = refreshIntervalSeconds;
  startClock();
  }
}
// -->
</script>

 

the code (part 2)

Place this image tag somewhere on your HTML page. Leave the question mark at the end of the image source because it is for non-caching trickery. If you have a different sized image, make sure you change the width and height attributes. Also make sure that your image src is correct for your webcam image.

<img src="webcam.gif?" name="webCamImage" alt="web cam" title="web cam" width="320" height="240" border="0">

 

the code (part 3)

This mini-form is for the countdown timer. Omitting this code, you will give you JavaScript errors unless you either make the timer element type="hidden" or you further modify the portion of the javaScript that displays the timer. See the instructions below for information on modifying this script.

<form name="webCam" action="#">
Refresh in: <input type="text" name="timer" size="2"> seconds.
</form>

 

the code (part 4)

To make the whole thing go, you need to add an onLoad atribute to the <body> tag. Your code should look something like this:

<body onLoad="startClock()">

the instructions

The simplest customizations to this code are for the web cam image source and the refresh interval (in seconds). They are found in Part 1 as:

var camImage = 'webcam.jpg';
var refreshIntervalSeconds = 60;

Make sure that the image source is correct, including the path needed to get there. The refresh interval should be pretty self explanitory.

 

To eliminate the dountdown timer, leave Part 3 out, and delete the following lines from the JavaScript in Part 1:

if (secondsLeft < 10) {
  document.webCam.timer.value = '0' + secondsLeft;
} else {
  document.webCam.timer.value = secondsLeft;
}

I tried to make the code very straight forward, but if you need any additional help making this code work for your project, please feel free to and I will be more than happy to give you a hand.

Wednesday, March 10, 2010 — 07:27 pm [PST]
©1966 — 2010 jovino (please do not steal)