The code is below.
How to add pause function and stop the timer, also set a button to pause the timer and resume when pressing resume?
//startTimer will start the timer onload of body.
function startTimer() {
userInput = "35";
if(userInput.length == 0){
alert("Please enter a value");
}
else {
var numericExpression = /^[0-9]+$/;
if(!userInput.match(numericExpression)){
alert("Please enter a number")
}
else {
function display( notifier, str ) {
document.getElementById(notifier).innerHTML = str;
}
function toMinuteAndSecond( x ) {
return Math.floor(x/60) + ":" + x%60;
}
function setTimer( remain, actions )
{
(function countdown()
{
display("countdown", toMinuteAndSecond(remain));
actions[remain] && actions[remain]();
(remain -= 1) >= 0 && setTimeout(arguments.callee, 1000);
})();
}
setTimer(userInput, {
35: function () { display("score", "6"); document.getElementById("total").value = 6 },
20: function () { display("score", "4"); document.getElementById("total").value = 4 },
15: function () { display("score", "3"); document.getElementById("total").value = 3 },
10: function () { display("score", "2"); display("notifier", "10 seconds left"); document.getElementById("total").value = 2 },
5: function () { display("score", "1"); display("notifier", "5 seconds left"); document.getElementById("total").value = 1 },
0: function () { display("score", "0"); display("notifier", "Time is up"); document.getElementById("total").value = 0 },
});
}
}
}
// setTimer sets the countdown and displays the time on the page.
//The setTimer(userInput) is to display score randomly on timer scope, as timer changes the score also changes.
On certain limit I need to pause the timer which will display correct score whenever I stop the timer.
Thank you for all of your help!!