Jump to content

'Javascript Automatic Image Slideshow' code is weak and not optimal


Nidhin Shankar

Recommended Posts

The code provided for 'Javascript Automatic Image Slideshow' code is weak and not optimal. It is bit confusing and does n't reflect the best of W3schools. Please check the link attached here(http://www.w3schools.com/w3css/w3css_slideshow.asp)

 

 

I suggest an edit.

Please compare the current code shown in w3schools(1) and my edited code(2):

 

1.
------------------------------------------------------------------------------------------------------------------------------------------------------------------

<script>
var myIndex = 0;
carousel();
function carousel() {
var i;
var x = document.getElementsByClassName("mySlides");
for (i = 0; i < x.length; i++) {
x.style.display = "none";
}
myIndex++;
if (myIndex > x.length) {myIndex = 1;}
x[myIndex-1].style.display = "block";
setTimeout(carousel, 2000); // Change image every 2 seconds
}
</script>
------------------------------------------------------------------------------------------------------------------------------------------------------------------
2.

<script>
var myIndex = 0;
var x = document.getElementsByClassName("mySlides");
carousel();
function carousel() {
if(myIndex!=0){x[myIndex-1].style.display="none";}
if (myIndex > (x.length-1)) {myIndex = 0}
x[myIndex].style.display = "block";
myIndex++;
setTimeout(carousel, 2000); // Change image every 2 seconds
}
</script>

------------------------------------------------------------------------------------------------------------------------------------------------------------------

Edited by Nidhin Shankar
Link to comment
Share on other sites

The examples provided are usually the simplest solutions that are possible. Once you understand how they work then -- yes -- you can easily improve them.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...