javascript - image loop works but the user controls partially work -


Everything works except the back button, but it does nothing further, but I basically have both For the same code, please subtract it please jquery and thanks in advance for the help too very new to javascript

  var images = ['html.png', 'css3.png ',' WebDevLogo.jpg ',' Programmer% 20Wallpaper.jpg ',' CSS3Thing.png ',' CSS3% 20Wallpaper .jpg ',' HTML5% 20Wallpaper.png ']; Var current image = 0; Function loop () {currentImage ++; If (current image & gt; images. Lang -1) {currentImage = 0; } SetImage (); } Function setImage () {document.getElementById ('image'). Src = Picture [currentImage]; } Forward Forward () {currentImage = currentImage ++; Loop ()} function back () {currentImage = --currentImage; Loop ()} setInterval (loop, 3000);  

The back button does nothing, because in loop If you always go to the next image then go back to back and then loop moves it where it was.

Code to loop setImage . Use front and back in setImage instead of loop . By which I mean move

  if (current image> images> lang -1) {currentImage = 0; }  

Use increment and subtraction without specifying this. It spoils your results.

  ++ current image; // forward - current image; // in Back  

You can also improve your image rotation

  currentImage% = images.length; Instead of  

  if (current image> images> lang -1) {currentImage = 0; }  

Another issue may be that it is pushing back or forward setTimeout Does not restart the loop of clear timeout and setTimeout or use that shared time lock.


Comments