0

I have a function that automatically loads when the pages opens. I want the function to stop running after "onclick". I want the slideshow to stop running automatically after clicking a button.

<html>
 <head>
 <script type="text/javascript">
 function changeImage(x){
 imageNumber += x;
 if(imageNumber > imageLength){
 imageNumber = 0;
 }
 if(imageNumber < 0){
 imageNumber = imageLength;
 }
 document.getElementById("slideshow").src = images[imageNumber];
 document.getElementById("caption").innerHTML = caption[imageNumber];
 return false;
 }
 function autoRun(){
 //This function simply makes the slideshow change slides every 5 seconds.
 setInterval("changeImage(1)", 5000);
 }
 function clearFunction(){
 //This function needs to stop the autoRun function. 
 //If the user wants to manual flip through the slides, 
 //I don't want the slides to continue changing automatically
 }
 </script>
 </head>
 <body onload="autoRun()">
 <a href="#" onclick="changeImage(-1); clearFunction();">Previous Slide</a>
 <a href="#" onclick="changeImage(1); clearFunction();">Next Slide</a>
 </body>
</html>
asked May 16, 2017 at 19:09
2
  • 1
    You can create a global variable. For example: var checkRun = true; You can check this variable in the function and set it to false in the first run. Commented May 16, 2017 at 19:12
  • Well the autoRun() must be doing setInterval of some kind. You need to maintain a reference to it outside autoRun() and use clearInterval in your clearFunction(). Please post the content of autoRun() Commented May 16, 2017 at 19:13

4 Answers 4

1

Intervals can be cleared:

var interval;
function autoRun(){
 //This function simply makes the slideshow change slides every 5 seconds.
 interval=setInterval(changeImage, 5000,1);
}
function clearFunction(){
 if(interval) clearInterval(interval);
}
answered May 16, 2017 at 19:19
Sign up to request clarification or add additional context in comments.

Comments

0

You can put all the code in autoRun() inside an if(flag) with flag being a boolean that is initially set to true and then you can set flag=false inside clearFunction(), so whenever clearFunction() is called, flag is set to false and the code inside autorun() won't execute because it won't pass the if(flag) check.

Note that flag needs to be a global variable.

answered May 16, 2017 at 19:14

Comments

0

You can make a window interval that calls autoRun every 5000 milliseconds.

// anywhere in your script, outside of any function
let timer = window.setInterval(changeImage, 5000);

In the clearFunction function, just do

window.clearInterval(timer);

See window.setInterval.

answered May 16, 2017 at 19:19

Comments

0

You need to use clearInterval

var interval = 1; 
 function autoRun(){
 //This function simply makes the slideshow change slides every 5 seconds.
 interval = setInterval(changeImage, 5000, 1);
 }
 function clearFunction(){
 clearInterval(interval);
 }
answered May 16, 2017 at 19:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.