javscript problems for slideshow...

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
curiousTek
Forum Newbie
Posts: 5
Joined: Fri Feb 04, 2005 11:44 am

javscript problems for slideshow...

Post by curiousTek »

I'm using the following code for a slideshow image on my site, trying to make it as easy to add images as possible without editing code. It changes images every 10 seconds perfect, the next() button works, but the prev() is giving me problems, it will go back a few times then say undefined, then go back to images.. but everyonce and ahwile it will say undefined.. I guess I must be math stupid or something..

here it is..

Code: Select all

<script language="JavaScript">
pix = new Array("images/mapusa.gif","images/mapusa.gif");
adtext = new Array("Refinance today","Work from home");

//preloading images
a = new Image();a.src = "images/mapusa.gif";
b = new Image(); b.src = "images/mapusa.gif";
//end preloading images

	var i = 0;

	function picadslide()&#123;
	setInterval("next()", 10000);
	&#125;

	function next()&#123;
	document.images.picad.src = pix&#1111;i];
	document.formad.formtext.value = adtext&#1111;i];
	i = i + 1;
	if (i > (pix.length - 1)) &#123;i = 0&#125;
	&#125;

	function prev()&#123;
	i = i - 1;
	if (i = (pix.length-(pix.length+1))) &#123;i = 1&#125;
	document.images.picad.src = pix&#1111;i];
	document.formad.formtext.value = adtext&#1111;i];
	&#125;

</script>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the logic in prev doesn't make sense:

Code: Select all

if (i = (pix.length-(pix.length+1))) &#123;i = 1&#125;
if set i to -1, then set i to 1...


next's logic can be modified to:

Code: Select all

i = (i + 1) % pix.length;
this will automatically handle the wrap-around.

prev's logic can be modified to:

Code: Select all

i = (i + pix.length - 1) % pix.length;
should also automatically handle the wrap-around in the reverse direction.

Not sure about undefined, as even with the screwy logic in prev, it should always show pix[1];



Moved to Client-Side.
Post Reply