Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I wrote the below code to create automatic slideshow . But the slideshow is not happening & only one image is showing all time. Please help

<!DOCTYPE html>
<html>
<head>
<title color= "#0101DF">Slide show</title>
<meta charset="UTF-8">
<meta name= "content" description="This is what Google will show you">
<link rel="icon" href= "https://qsf.ec.quoracdn.net/-3-images.favicon.ico-26-1285e84414ca1091.ico" />
<script>
pictures=["Penguins.jpg","saran.jpg","Tulips.jpg"];
function settime() {
setInterval(load,1000);
}
function load() {
for (i=0;i<pictures.length;i++){
document.getElementById("img1" ).src=pictures[i];
//setTimeout(load,1000);
} 
}

</script>

</head>
<center> 
<body bgcolor="#E6E6FA" onload=settime()>
<h1> My Pictures </h1>
<img id="img1" style="height:100%;width:100%" />

</body>
</center> 
</html>


What I have tried:

Hi,
I wrote the below code to create automatic slideshow . But the slideshow is not happening & only one image is showing all time. Please help

<!DOCTYPE html>
<html>
<head>
<title color= "#0101DF">Slide show</title>
<meta charset="UTF-8">
<meta name= "content" description="This is what Google will show you">
<link rel="icon" href= "https://qsf.ec.quoracdn.net/-3-images.favicon.ico-26-1285e84414ca1091.ico" />
<script>
pictures=["Penguins.jpg","saran.jpg","Tulips.jpg"];
function settime() {
setInterval(load,1000);
}
function load() {
for (i=0;i<pictures.length;i++){
document.getElementById("img1" ).src=pictures[i];
//setTimeout(load,1000);
} 
}

</script>

</head>
<center> 
<body bgcolor="#E6E6FA" onload=settime()>
<h1> My Pictures </h1>
<img id="img1" style="height:100%;width:100%" />

</body>
</center> 
</html>
Posted
Updated 22-Nov-17 0:15am

Every time your load function fires you go through all pictures in a loop and set the source, so every time you end up with the last image in the array. Instead you want to go through them only once every time the timer fires (I assume). So keep an index count somewhere of the current picture and increment it in the load function.

var index = 0;
pictures = ["Penguins.jpg", "saran.jpg", "Tulips.jpg"];
function settime() {
    setInterval(load, 1000);
}
function load() {
    document.getElementById("img1").src = pictures[index];

    index++;

    if (index >= pictures.length) {
        index = 0;
    }
}
 
Share this answer
 
Comments
[no name] 22-Nov-17 6:17am    
index >= pictures.length can be changed to index == pictures.length as index is always incremented by 1 and can never satisfy '>'.
F-ES Sitecore 22-Nov-17 6:22am    
And the index++ line isn't needed at all, it can be moved to the line above, but the guy is obviously a newbie so it's better to focus on making code explicit about what it's doing than wondering about optimising etc.
There is nothing to debug syntax wise, but logically, this:
for (i=0;i<pictures.length;i++){
document.getElementById("img1" ).src=pictures[i];
//setTimeout(load,1000);
} 
will loop thru all the 3 pictures and end with the last picture that you will always see.
You have to rewrite you code.
You have to understand what you are doing, don't just copy and paste.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900