Forum Moderators: open

Message Too Old, No Replies

How about this, whats wrong with this simple script?

         

Simone100

7:00 am on Oct 22, 2006 (gmt 0)

10+ Year Member



Hello, having trouble with the simple script. Trying to get the setTimeout to
refire the script so that an item changes every minute. But the setTimout isn't working. Anyone know why? It works fine on refresh, just not changing on its own. Thanks you very much....

arr = new Array(
["http://www.google.com/some.img"]
);

now = new Date()
nowDate = now.getMinutes()

function change(){
if(nowDate > arr.length) { return; }
document.getElementById("img").src = arr[nowDate-1][0];
setTimeout('change()',1000);
}

Simone100

8:51 am on Oct 22, 2006 (gmt 0)

10+ Year Member



I think it has to do with the element by id not including the setTimout, but I don't know how to have it include it.

daveVk

9:21 am on Oct 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if(nowDate > arr.length) { return; }

if condition is true, function change will exit without reseting timeout. Reorder statements like this maybe?

function change(){
setTimeout('change()',1000);
if(nowDate > arr.length) { return; }
document.getElementById("img").src = arr[nowDate-1][0];
}

Not sure what code is intended to as there is only one image in array and nowDate does not appear to change between timeouts?

Simone100

9:42 am on Oct 22, 2006 (gmt 0)

10+ Year Member



Nope it didn't like it but thanks for help, this one is driving me crazy I have spent several days on it already. All the other examples I have seen put the setTimeout in the same function at the end, including the function in it as well. But I don't think it works with this. Because the call element by ID is calling the on the nowDate and totatally ignoring the setTimout.

Could I include another "if" statement? Something that says if element by id does this then setTimeout? Anyone have a good idea on how to do that? Thanks.

kaled

10:12 am on Oct 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) Variables should be declared using var.
2) Code that always returns the same answer should be called once only.
3) When using loops, make sure you're updating all the right vars (within your loop, there are no useful assignments!)

I'm not sure what you're trying to achieve, but your code would have to look something a bit more like this...

var arr = new Array(
["http://www.google.com/some.img"]
);
var now = new Date();
var img = document.getElementById("img");

function change(){
var nowDate = now.getMinutes()
if (nowDate > arr.length) { return; }
img.src = arr[nowDate-1][0];
setTimeout('change()',1000);
}

However, I am certainly not saying this will will do anything useful.

Kaled.

Simone100

12:41 pm on Oct 22, 2006 (gmt 0)

10+ Year Member



Thanks a lot. Still having trouble, I am going to try something else.
No need to help me any more on this one, thanks.