To create a 10-minute timer, set the target time for 10 minutes in the future and use either setInterval()
or setTimeout()
to decrement and display the time. Convert the milliseconds to minutes and seconds for display purposes.
let countdownTime = 10 * 60 * 1000; // 10 minutes in milliseconds
let endTime = new Date().getTime() + countdownTime;
let timer = setInterval(function() {
let currentTime = new Date().getTime();
let timeLeft = endTime - currentTime;
if (timeLeft <= 0) {
clearInterval(timer);
console.log("10-minute timer complete!");
} else {
let minutes = Math.floor((timeLeft % (1000 * 60 * 60)) / (1000 * 60));
let seconds = Math.floor((timeLeft % (1000 * 60)) / 1000);
console.log(`Time left: ${minutes}m ${seconds}s`);
}
}, 1000);