Multiple javascript slideshows - need help defining different slideshow lengths

The code below works only when all slide shows are the same length ie they contain the same amount of pictures.  A blank image appaears in the slideshow with three images since the other slide show before it has four images.  I need a code that will make it possible to run these slideshows with different numbers of image snad will not show blank spots.  Can anyone help.  This is the code.  Thanks

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin

NewImg1 = new Array (
"images/callisto.jpg",
"images/io.jpg",
"images/ganymede.jpg",
"images/europa.jpg"
);

NewImg2 = new Array (
"images/io.jpg",
"images/callisto.jpg",
"images/ganymede.jpg"
);

NewImg3 = new Array (
"images/ganymede.jpg",
"images/callisto.jpg",
"images/io.jpg",
"images/ganymede.jpg",
"images/callisto.jpg",
"images/io.jpg",
"images/europa.jpg"
);





var ImgNum = 0;
var ImgLength = NewImg1.length - 1




//Time delay between Slides in milliseconds
var delay = 3000;

var lock1 = false;
var lock2 = false;
var lock3 = false;
var run1;
var run2;
var run3;





function chgImg(show,direction) {
  if (document.images) {
   ImgNum = ImgNum + direction;
   if (ImgNum > ImgLength) { ImgNum = 0; }
   if (ImgNum < 0) {ImgNum = ImgLength;  }
   if (show == 'show1' ){ document.slideshow1.src = NewImg1[ImgNum]; }
   if (show == 'show2' ){ document.slideshow2.src = NewImg2[ImgNum]; }
   if (show == 'show3' ){ document.slideshow3.src = NewImg3[ImgNum]; }
  }
}


function auto(show) {
if ( show == 'show1' ){
  if (lock1 == true) {
    lock1 = false;
    window.clearInterval(run1);
  } else if (lock1 == false) {
      lock1 = true;
      run1 = setInterval("chgImg('show1',1)", delay);
  }
}
if ( show == 'show2' ){
  if (lock2 == true) {
    lock2 = false;
    window.clearInterval(run2);
  } else if (lock2 == false) {
      lock2 = true;
      run2 = setInterval("chgImg('show2',1)", delay);
  }

}
if ( show == 'show3' ){
  if (lock3 == true) {
    lock3 = false;
    window.clearInterval(run3);
  } else if (lock2 == false) {
      lock3 = true;
      run3 = setInterval("chgImg('show3',1)", delay);
  }
}

}

//  End -->
</script>

</HEAD>


<BODY>
<center>
<img src="images/callisto.jpg" name="slideshow1" width="262" height="262">
<table>
<tr>
<td align="right"><a href="javascript:chgImg('show1',-1)">Previous</a></td>
<td align="center"><a href="javascript:auto('show1')">Auto/Stop</a></td>
<td align="left"><a href="javascript:chgImg('show1',1)">Next</a></td>
</tr>
</table>
</center>

<center>
<img src="images/io.jpg" name="slideshow2" width="262" height="261">
<table>
<tr>
<td align="right"><a href="javascript:chgImg('show2',-1)">Previous</a></td>
<td align="center"><a href="javascript:auto('show2')">Auto/Stop</a></td>
<td align="left"><a href="javascript:chgImg('show2',1)">Next</a></td>
</tr>
</table>
</center>

<center>
<img src="images/ganymede.jpg" name="slideshow3" width="262" height="261">
<table>
<tr>
<td align="right"><a href="javascript:chgImg('show3',-1)">Previous</a></td>
<td align="center"><a href="javascript:auto('show3')">Auto/Stop</a></td>
<td align="left"><a href="javascript:chgImg('show3',1)">Next</a></td>
</tr>
</table>
</center>





</BODY>
</HTML>
dialektkidAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BustaroomsCommented:
without changing your code too much you can add this

var theLength=(show == 'show1') ? NewImg1.length -1 : (show == 'show2') ? NewImg2.length - 1 : NewImg3.length-1;


in your chgImg function before the if(document.images)
0
BustaroomsCommented:
oh and you need to change something else....here the new chgImg function

function chgImg(show,direction) {
var theLength=(show == 'show1') ? NewImg1.length -1 : (show == 'show2') ? NewImg2.length - 1 : NewImg3.length-1;
 if (document.images) {
  ImgNum = ImgNum + direction;
  if (ImgNum > theLength) { ImgNum = 0; }
  if (ImgNum < 0) {ImgNum = theLength;  }
  if (show == 'show1' ){ document.slideshow1.src = NewImg1[ImgNum]; }
  if (show == 'show2' ){ document.slideshow2.src = NewImg2[ImgNum]; }
  if (show == 'show3' ){ document.slideshow3.src = NewImg3[ImgNum]; }
 }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
apprentiCommented:
var ImgLength = NewImg1.length - 1

This variable is being used as the length for all the slideshows.
0
apprentiCommented:
Sometimes it can be worth doing a little cut and paste, and making separate functions with different variable names in each. If each slideshow is controlled by a separate function, there's less chance of this sort of problem arising.
0
bhatiashardeepCommented:
Check out with some changes in your code only
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">

NewImg1 = new Array (
"temp_files/1.jpg",
"temp_files/2.jpg",
"temp_files/3.jpg",
"temp_files/4.jpg"
);

NewImg2 = new Array (
"temp_files/1.jpg",
"temp_files/2.jpg",
"temp_files/3.jpg"
);

NewImg3 = new Array (
"temp_files/1.jpg",
"temp_files/3.jpg",
"temp_files/2.jpg",
"temp_files/4.jpg",
"temp_files/1.jpg",
"temp_files/2.jpg",
"temp_files/3.jpg"
);





var ImgNum1 = 0;
var ImgNum2 = 0;
var ImgNum3 = 0;
var ImgLength = NewImg1.length - 1




//Time delay between Slides in milliseconds
var delay = 1000;

var lock1 = false;
var lock2 = false;
var lock3 = false;
var run1;
var run2;
var run3;





function chgImg(show,direction) {
 
  if (document.images) {
   if (show == 'show1' ){
   document.slideshow1.src = NewImg1[ImgNum1];
   if(ImgNum1==NewImg1.length-1)
            ImgNum1=0
      else
            ImgNum1++;            
    }
   if (show == 'show2' ){
   document.slideshow2.src = NewImg2[ImgNum2];
   if(ImgNum2==NewImg2.length-1)
            ImgNum2=0
      else
            ImgNum2++;
   }
   if (show == 'show3' ){
   document.slideshow3.src = NewImg3[ImgNum3];
      if(ImgNum3==NewImg3.length-1)
            ImgNum3=0
      else
            ImgNum3++;
   }
  }
}


function auto(show) {
if ( show == 'show1' ){
  if (lock1 == true) {
    lock1 = false;
    window.clearInterval(run1);
  } else if (lock1 == false) {
      lock1 = true;
      run1 = setInterval("chgImg('show1',1)", delay);
     
  }
}
if ( show == 'show2' ){
  if (lock2 == true) {
    lock2 = false;
    window.clearInterval(run2);
  } else if (lock2 == false) {
      lock2 = true;
      run2 = setInterval("chgImg('show2',1)", delay);
  }

}
if ( show == 'show3' ){
  if (lock3 == true) {
    lock3 = false;
    window.clearInterval(run3);
  } else if (lock2 == false) {
      lock3 = true;
      run3 = setInterval("chgImg('show3',1)", delay);
  }
}

}


</script>

</HEAD>


<BODY>
<center>
<img src="images/callisto.jpg" name="slideshow1" width="262" height="262" id="slideshow1">
<table>
<tr>
<td align="right"><a href="javascript:chgImg('show1',-1)">Previous</a></td>
<td align="center"><a href="javascript:auto('show1')">Auto/Stop</a></td>
<td align="left"><a href="javascript:chgImg('show1',1)">Next</a></td>
</tr>
</table>
</center>

<center>
<img src="images/io.jpg" name="slideshow2" width="262" height="261" id="slideshow2">
<table>
<tr>
<td align="right"><a href="javascript:chgImg('show2',-1)">Previous</a></td>
<td align="center"><a href="javascript:auto('show2')">Auto/Stop</a></td>
<td align="left"><a href="javascript:chgImg('show2',1)">Next</a></td>
</tr>
</table>
</center>

<center>
<img src="images/ganymede.jpg" name="slideshow3" width="262" height="261" id="slideshow3">
<table>
<tr>
<td align="right"><a href="javascript:chgImg('show3',-1)">Previous</a></td>
<td align="center"><a href="javascript:auto('show3')">Auto/Stop</a></td>
<td align="left"><a href="javascript:chgImg('show3',1)">Next</a></td>
</tr>
</table>
</center>





</BODY>
</HTML>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.