Page 1 of 1

javscript problems for slideshow...

Posted: Sat Feb 05, 2005 4:08 pm
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>

Posted: Sat Feb 05, 2005 4:36 pm
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.