I am trying to stop my setInterval()
but nothing I tried has worked.
what do I need to put in my clearInterval
to stop the setInterval()
function from running.
html
<p>PLease enter a time(secs) interval to have the divs change colors.</p>
Interval: <input type='number' id='interval' min='1' required>
<input type='button' value='Set Time' onclick='setTime()'>
<br><br>
<p> click the button below to stop it.</p>
<button onclick="clearInterval(x)">Stop it</button>
javascript
function setTime(){
var userTime = document.getElementById('interval').value * 1000;
if (userTime != null && userTime > 0 ){
var x = setInterval(colorChangeTime, userTime);
} else {
alert("Please enter a number");
}
the only thing I get with clearInterval(x)
is a console error "x is undefined". I have tried other various things but haven't gotten anything to work.