0

Hi all;
i have script below
i would like to ask
why there var step=1; outside the function works but inside not
anyone explain me pls
thanks beforehands

<html><head>
<script type="text/javascript"> 
<!--
var image1=new Image()
image1.src="bir.jpg"
var image2=new Image()
image2.src="iki.jpg"
var image3=new Image()
image3.src="uc.jpg"
//-->
</script>
</head>
<body>
<img src="bir.jpeg" name="slide" width="100" height="56" />
<script> 
<!--
//variable that will increment through the images
 
function slideit(){
var step=1;
//if browser does not support the image object, exit.
if (!document.images)
return
document.images.slide.src=eval("image"+step+".src")
if (step<3)
step++
else
step=1
//call function "slideit()" every 2.5 seconds
setTimeout("slideit()",2500)
}
slideit()
//-->
</script>
</body>
</html>
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by azegurb
0

A few modifications should be made to your code:

>> Each codeline needs to be ended with a ; >> Line 15 - You forgot to add the script-type, it needs to be <script type="text/javascript"> >> Line 20 - Each time the function gets called, the step gets set to 2. It needs to be outside the function, else the increment in line 26 is useless and the image won't slide


>> Line 24 - It is not possible to adress a variable using a string, use the code I have written on the bottom of the post

>> Line 25 till 27 - Although it is shorter, keep using the accoladdes {} to start and end a statement.

My version of your function slideit():

var step=1;
function slideit(){
//if browser does not support, don't bother switching the image
if (document.images) {
switch (step) {
case 1 :
document.images.slide.src = image1.src;
break;

case 2 :
document.images.slide.src = image2.src;
break;

case 3 :
document.images.slide.src = image3.src;
break;
}
if (step<3) {
step++;
} else {
step = 1;
}
//call function "slideit()" every 2.5 seconds
setTimeout("slideit()",2500)
}
}

~G

Edited by Graphix: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.