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>

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

thank you very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.