Two input field "minute" and "second" is given. The value of the "second" field can exceed 60. A stopwatch needs to be created starting from the above timings.
Eg. If the minute is 5 and the second is 30. The stopwatch will start from 5:30 and then it will go on until both minute and second become 0.
Eg. If the minute is 5 and the second is 90. The stopwatch will start from 6:30 and then it will go on until both minute and second become 0.
There will be three-button with the "RESET", "PAUSE/RESUME" and "START" buttons.
RESET will reset the stopwatch to the user-given field. PAUSE will pause the stopwatch and if clicked again it will resume it. START will start the stopwatch at the initial go.
I am finding it difficult to create such a timer. Please help.
To Start timer you can follow the steps mentioned below. It will help you.
Step 1: Minutes = 5 and Seconds = 90
Step 2: Convert all total minutes and seconds into total seconds
const min = 5;
const seconds = 90;
const totalSeconds = (min * 60) + seconds;
Step 3: Create the method that runs the timer.
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
const interval = setInterval(function () {
minutes = parseInt(timer / 60, 10);
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
console.log(minutes + ":" + seconds);
timer = timer - 1;
if (timer < 0) {
clearInterval(interval);
}
}, 1000);
}
Step 4: Call this method on Start-Timer button click