var interval;
button.onclick = function () {
interval = setInterval(slideShow, 2000); // assigned to a variable
}
You definitely need a variable
to be assigned to the setInterval
, which can be stopped using
clearInterval(interval);
How can I stop the interval using the same button I used to start the interval?
You do this by setting up a boolean
check flag like
var isRecursiveOn = true; // by default it will be true
var interval;
button.onclick = function () {
if (isRecursionOn) { // check the flag
isRecursiveOn = false;
interval = setInterval(slideShow, 2000); // assigned to a variable
} else {
clearInterval(interval);
isRecursiveOn = true; //reset it
}
}