Reputation: 1
I have the following code:
<script type="text/javascript">
var image1=new Image()
image1.src="images/natandadam.jpg"
var image2=new Image()
image2.src="images/gardaa.jpg"
var image3=new Image()
image3.src="images/marmaris.jpg"
var image4=new Image()
image4.src="images/gardab.jpg"
var image5=new Image()
image5.src="images/engagement.jpg"
var image6=new Image()
image6.src="images/gardac.jpg"
var image7=new Image()
image7.src="images/natandadamlake.jpg"
//-->
</script>
<script>
<!--
//variable that will increment through the images
var step=1
function slideit(){
//if browser does not support the image object, exit.
if (!document.images) {
return;
}
document.images.slide.src=eval("image"+step+".src");
if (step<7) {
step++;
} else {
step=1;
}
//call function "slideit()" every 2.5 seconds
setTimeout("slideit()",2500);
}
slideit();
//-->
</script>
...and this calls it:
<img style="border:6px double #545565;" src="gardaa.jpg" name="slide" width=600 height=400>
I want it to continue skipping through the images but it stops on the last one. It used to repeat when it only had three images; Now I've added more its stopped.
what do I change to make them repeat?
Upvotes: 0
Views: 1830
Reputation: 114367
You can simplify your script quite a bit:
<script type="text/javascript">
var images ="natandadam.jpg,gardaa.jpg,marmaris.jpg,gardab.jpg,engagement.jpg,gardac.jpg,natandadamlake.jpg".split(",")
var step=1
function slideit(){
document.images.slide.src="images/" + images[step]
step++
if(step>images.length) {
step = 1
}
setTimeout(function(){slideit()},2500);
}
slideit();
</script>
A few tips:
1 - you don't need to use <!--
and //-->
to hide your script. The only browser that required this died in the 90s.
2 - don't use eval()
, it's evil.
3 - since the images are now in an array, you can check add or remove and many as you wish without having to use if (step<7)
. The code just inspects the length of the array.
4 - Use what is called an anonymous function in setTimeout instead of quoting code. Quoting code uses eval(
) internally.
Upvotes: 3