This select random times for the setTimeout. The myFunction() calls itself. It works correctly when I click the “start” button the first time, but when I click “stop” the “start” again it goes crazy after timing several numbers correctly. It is supposed to display a number, for example, 7, for 7 seconds then randomly choose maybe 3 and display 3 for 3 seconds. For whatever reason after I start/stop it will after a few numbers, start display a 5 but then a second later display a new number without a delay.
Any ideas, please?
“`
<html><body>
<BR><BR>
<button id=startIt>Start</button>
<button id=stopIt>Stop</button>
<p id=”demo”></p>
<script>
function myFunction() {
var min = 0, max = 8;
var rand = Math.floor(Math.random() * (max – min + 1) + min); //Generate Random number between min/max
setTimeout(myFunction, rand * 1000);
document.getElementById(“demo”).innerHTML = (rand + ” ” +”<BR>”);
}
startIt.onclick = function(){this.disabled=true;myFunction()}
stopIt.onclick = function(){startIt.disabled=false;rand=0;}
</script>
</body> </html>