0

I am trying to make it so that there are two buttons using Javascript, one to stop the image carousel using clearInterval which works well, however, I also want another button to restart the carousel however I can't figure out how to do so.

<img src="images/img1.jpg" id="images" width="200px"> <button type="button" id="stop">Stop the Carousel</button> <button type="button" id="start">Start the Carousel</button> <script> document.getElementById('stop').addEventListener('click', stopit); var start = 1; var timer = setInterval(carousel, 2000); function carousel(){ var image_data; start = start % 5; image_data = "images/img" + (start+1) + ".jpg"; document.getElementById('images').src=""+ image_data; start++; } function stopit(){ clearInterval(timer); } </script> 

1
  • Just put your current code into a click handler of a start button. Commented May 23, 2019 at 9:16

2 Answers 2

1

Move your timer start logic to its own function:

<img src="images/img1.jpg" id="images" width="200px"> <button type="button" id="stop">Stop the Carousel</button> <button type="button" id="start">Start the Carousel</button> <script> document.getElementById('stop').addEventListener('click', stopit); document.getElementById('start').addEventListener('click', startIt); var start = 1; var timer function carousel(){ var image_data; start = start % 5; image_data = "images/img" + (start+1) + ".jpg"; document.getElementById('images').src=""+ image_data; start++; } function startIt(){ if(timer) stopit() timer = setInterval(carousel, 2000); } function stopit(){ clearInterval(timer); } startIt() //if you want it to start automatically </script> 
Sign up to request clarification or add additional context in comments.

2 Comments

What if you click twice on start?
"...another button to restart the carousel" - It seems carousel should start by default on page load.
0

Please use this code there start, stop and restart button as well I hope it's useful.

if you click restart button then first carousel image comes it means it's work beginning.

Thanks

<img src="images/img1.jpg" id="images" width="200px"> <button type="button" id="stop">Stop the Carousel</button> <button type="button" id="start">Start the Carousel</button> <button type="button" id="restart">Restart the Carousel</button> <script> document.getElementById('stop').addEventListener('click', stopit); document.getElementById('restart').addEventListener('click', restart); document.getElementById('start').addEventListener('click', start); var start = 1; var timer = setInterval(carousel, 2000); function carousel(){ var image_data; start = start % 5; image_data = "images/img" + (start+1) + ".jpg"; document.getElementById('images').src=""+ image_data; start++; } function stopit(){ clearInterval(timer); } function start(){ timer = setInterval(carousel, 2000); } function restart(){ start = 1; timer = setInterval(carousel, 2000); } </script> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.