Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
60
rated 0 times [  62] [ 2]  / answers: 1 / hits: 15112  / 8 Years ago, tue, september 6, 2016, 12:00:00

Currently closee to finishing a slideshow using html and JavaScript. My last problem is creating stop button which needs to use a return function (I assume) Or some sort of exit function. It is an image slideshow that runs automatically, can be paused, skip image, go back an image and so on. I'd like the stop button to kill the autoRun function and set the image back to the first default image. I've set up a function which I'm guessing is totally wrong as it is not working.



The HTML





  <td class=controls>

<button onClick=autoRun()>Start</button>
<button onClick=changeImage(-1); return false;>Previous Image</button>
<button onClick=pause();>pause</button>
<button onClick=changeImage(1); return false;>Next Image</button>
<button onClick=Exit();>Exit</button>



</td>

</tr>


All buttons are working besides the last one



JavaScript



var images = [HGal0.jpg, HGal1.jpg, HGal2.jpg, HGal3.jpg, HGal4.jpg, HGal5.jpg, HGal6.jpg, HGal7.jpg, HGal8.jpg, HGal9.jpg, HGal10.jpg, HGal11.jpg, HGal12.jpg, HGal13.jpg, HGal14.jpg, HGal15.jpg];
var interval = setInterval(changeImage(1), 2000);
var imageNumber = 0;
var imageLength = images.length - 1;

function changeImage(x) {
imageNumber += x;
// if array has reached end, starts over
if (imageNumber > imageLength) {
imageNumber = 0;
}
if (imageNumber < 0) {
imageNumber = imageLength;
}

document.getElementById(slideshow).src = images[imageNumber];

return false;
}

function autoRun() {
interval = setInterval(changeImage(1), 2000);

}

function pause(){
clearInterval(interval);
interval = null;
}

function Exit(){
return;
}


I'm not fully understanding the return statement in the Exit function, as most examples I've looked at run the function when an 'if' statement is met, whereas I'd like mine to execute when the Stop button is clicked. Thanks


More From » return

 Answers
46

A return statement simply exits the function in which it appears, it doesn't cause other things to stop. So this:



function Exit(){
return;
}


...has the same effect as this:



function Exit() { }


That is, the function doesn't do anything at all.




I'd like the stop button to kill the autoRun function and set the image back to the first default image.




OK, so have your Exit() function call your other functions:



function Exit() {
pause(); // this will stop the slideshow
imageNumber = 0; // reset to the first image
changeImage(0); // change to that image
}

[#60800] Friday, September 2, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jerome

Total Points: 592
Total Questions: 98
Total Answers: 101

Location: Tonga
Member since Tue, Nov 30, 2021
3 Years ago
jerome questions
Thu, Jan 27, 22, 00:00, 2 Years ago
Fri, Oct 22, 21, 00:00, 3 Years ago
Sat, Sep 19, 20, 00:00, 4 Years ago
Fri, Sep 6, 19, 00:00, 5 Years ago
;