0

I want to pause timer when the value in the answer box is 119.
I'm not able to pause the timer or clearInterval when the value is 119, the timer won't pause.

<body onload="document.getElementById('box1').focus, startTimer(), pause()">
<input onkeyup="sum(),secret()" id="box1" autofocus/><br><br>

<input onkeyup="sum(),secret()" id="box2"/><br><br>

<input id="ans" disabled/>
<p id="secret" style="display: none;">
Time paused
</p>
<script>
function secret() {
  if(document.getElementById('ans').value == "119") {
    document.getElementById('secret').style.display = "block";
  }
  else {
  document.getElementById('secret').style.display = "none";
  }
}
function sum() {
var x = document.getElementById('box1').value;
var y = document.getElementById('box2').value;
var a = +x + +y;
  document.getElementById('ans').value = a;
  }
function startTimer() {
    var fiveMinutes = 30,
        display = document.getElementById("time"),
        mins, seconds;
   var tt = setInterval(function() {
        mins = parseInt(fiveMinutes / 60)
        seconds = parseInt(fiveMinutes % 60);
        seconds = seconds < 10 ? "0" + seconds : seconds;
        display.innerHTML = mins + ":" + seconds;
        fiveMinutes--;

        if (fiveMinutes < 0) {
            document.getElementById('timeup').style.display = "block";
            clearInterval(tt);
        }
    }, 1000);
    }
function pause() {
  setInterval(pau, 10);
}
function pau() {
  if(document.getElementById('secret').style.display == "block") {
    }
}
</script>
<br>
<br>
<div onchange="pause()" id="time"></div>
    <p id="timeup" style="display: none;">Time up </p>
</body>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Moltres
  • 600
  • 4
  • 21
  • You can't stop a `setInterval()` and then start a new one later. You can't "pause" an existing timer and then start the same one again. You can stop one and then create a new one later to start again. – jfriend00 Feb 18 '15 at 06:41
  • Check out this link, where a pausable timout is created: http://stackoverflow.com/questions/3969475/javascript-pause-settimeout – Inactive Feb 18 '15 at 07:22

0 Answers0