...

View Full Version : Problem with in line JS commands



dobbies
06-23-2012, 03:17 PM
Hi

I have a single webcam image that's refreshed every 5 secs with some js code which works fine. However I'm now attempting two images on the same page and sort of doubling up the code but changing the file names. Here's the code:-

<div><img src="http://www.my_website/" width="450" height="350" alt="" name="dog1" ></div>
<script language="JavaScript" type="text/javascript" >
<!--
var images = new Array();
images[1] = "dogs.jpg";
interval = 5000;

function Refresh() {
for (i=1; i<=1; i++) {
tmp = new Date();
tmp = "?" + tmp.getTime();
document.images["cam" + i].src = images[i] + tmp;
}
setTimeout("Refresh()", interval);
}

Refresh();
// -->
</script>

<div><img src="http://www.My_website/" width="375" height="300" alt="" name="cam0" ></div>
<script language="JavaScript" type="text/javascript">
<!--
var tcimages = new Array();
tcimages[0] = "cats.jpg";
interval = 5000;

function Refresh() {
for (i=0; i<=0; i++) {
tmp = new Date();
tmp = "?" + tmp.getTime();
document.images["cat" + i].src = tcimages[i] + tmp;
}
setTimeout("Refresh()", interval);
}

Refresh();
// -->
</script>

My problem is that the result of using the code in this cofiguration results in the first image remaining static (not refreshing) and the second image refreshing OK. I can't seem to get both images refreshing so some help would be gratefully appreciated. Please keep it simple as I'm a js newbie.

Many thanks

Lerura
06-23-2012, 03:28 PM
your first images have name="dog1" but you refer to it by document.images["cam" + i].src where it should be document.images["dog" + i].src

dobbies
06-23-2012, 07:02 PM
Thank you for your reply.

I appologise for the typo's, I changed some of the actual names in the code and made some stupid mistakes. Below is the actual code for scrutiny, the problem is still apparent and any help would be appreciated....



<div><img src="http://www.jacarandatree.co.uk/" width="450" height="350" alt="" name="uk1" ></div>
<script language="JavaScript" type="text/javascript" >
<!--
var images = new Array();
images[1] = "cornwall.jpg";
interval = 5000;

function Refresh() {
for (i=1; i<=1; i++) {
tmp = new Date();
tmp = "?" + tmp.getTime();
document.images["uk" + i].src = images[i] + tmp;
}
setTimeout("Refresh()", interval);
}

Refresh();
// -->
</script>

<div><img src="http://www.jacarandatree.co.uk/" width="375" height="300" alt="" name="es0" ></div>
<script language="JavaScript" type="text/javascript">
<!--
var tcimages = new Array();
tcimages[0] = "tincam.jpg";
interval = 5000;

function Refresh() {
for (i=0; i<=0; i++) {
tmp = new Date();
tmp = "?" + tmp.getTime();
document.images["es" + i].src = tcimages[i] + tmp;
}
setTimeout("Refresh()", interval);
}

Refresh();
// -->
</script>

Lerura
06-23-2012, 09:37 PM
You have 2 functions named Refresh.
The result is that the second function overwrites the first.

Try renaming one of the functions, together with the related initializing call.
eg. to Refresh1

felgall
06-23-2012, 09:42 PM
If the functions were defined using Refresh = function() { instead of function Refresh() { then they'd actually work independently with the second not overwriting the first until after the first had been run. Then they'd work as required.

Defining functions with the word function first doesn't always work as expected (there are even places where you can't define functions that way) but the other way around with the word function after the = always works.

A better solution to this particular situation would be to pass "uk" and "es" into the function as a parameter so that the function doesn't need to be duplicated at all.

dobbies
06-24-2012, 08:14 PM
I tried Felgall's option and this didn't work so I then tried Lerura's solution and this did, so problem solved.

I will spend more time with Felgall's option to hopefully learn more.

Many thanks to you both and the forum for this great service.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum