...

View Full Version : seTimeout works but then just stops



boxxer03
05-05-2006, 01:10 AM
This is making me mad and I know its some stupid mistake I made cause I was just messing around when I made this. But its supposed to display the images one after another, which it does. Until its on the 3rd image, the timeout works fine for the first two rounds btu the third one it creates an error which I checked in Firefox and its saying tha imageSwap() in the timeout is not a valid function (basically). So I was wondering how come it'll show the first two images but fail on the third? I've tried pretty much everything and its some stupid little error thats messing it all up.


<script type="text/javascript">
var images=new Array()
images[0]='image1.jpg'
images[1]='image2.jpg'
images[2]='image3.jpg'

var current_image=0;

function imageSwap(){
document.write('<img src="'+images[current_image]+'" alt="'+images[current_image]+'">')
current_image=current_image+1

var resetSwap=(current_image==images.length)?current_image=0:"";
setTimeout("imageSwap()",3000);
}
</script>

A1ien51
05-05-2006, 03:46 AM
This is wrong:

var resetSwap=(current_image==images.length)?current_image=0:"";

you do not set a variable in there and save it to a variable. Use a normal if statement!

Eric

boxxer03
05-05-2006, 09:21 AM
Actually that doesn't matter. I tried just breaking it down into a normal if statement and even completely removing it and it stills tops on the 3rd round.

boxxer03
05-05-2006, 09:23 AM
I've tried removing everything except the document.write("") line just to see if it would keep posting the same image and it did, twice, but on the 3rd time it errored.

ca_redwards
05-05-2006, 06:34 PM
Your problem is that you're delaying the document.write()

If you wait too long, the browser will attempt to rewrite the entire page, first by dumping the DOM of the currently displayed [stale] page. A better way to accomplish the desired effect is to periodically change the src attribute or even its visibility property.


This is making me mad and I know its some stupid mistake I made cause I was just messing around when I made this. But its supposed to display the images one after another, which it does. Until its on the 3rd image, the timeout works fine for the first two rounds btu the third one it creates an error which I checked in Firefox and its saying tha imageSwap() in the timeout is not a valid function (basically). So I was wondering how come it'll show the first two images but fail on the third? I've tried pretty much everything and its some stupid little error thats messing it all up.


<script type="text/javascript">
var images=new Array()
images[0]='image1.jpg'
images[1]='image2.jpg'
images[2]='image3.jpg'

var current_image=0;

function imageSwap(){
document.write('<img src="'+images[current_image]+'" alt="'+images[current_image]+'">')
current_image=current_image+1

var resetSwap=(current_image==images.length)?current_image=0:"";
setTimeout("imageSwap()",3000);
}
</script>

boxxer03
05-05-2006, 07:19 PM
I'll try that. I was just thinking about that and wondering if it would work, so i'll try it and see what happens but that should be it, because if you remove everything and leave the document.write("") then it still errors. Thanks alot.

boxxer03
05-05-2006, 07:26 PM
Yeah, that was it. The one thing I didn't try to fix it. Thanks a lot though to both you guys for your help.

A1ien51
05-05-2006, 07:51 PM
I totally missed the fact you were using document.write

Eric



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum