DEV Community

CodeMonk
CodeMonk

Posted on

Setting countdown timer in JS -oneliners

const logoutTimer = function () {
let time = 120;
//setting it to 2 min
const timer = setInterval(()=> {
const min = String(Math.trunc(time/60)).padStart(2, 0);
const sec = time % 60;
labelTimer.textContent = ${min}min:${sec}sec;
//use your label where you want to place it
time--;
if (time === 0) {
clearInterval(timer);
//this will end up your timer when it reaches 0sec
//place your function here
}
}, 1000);
};

Top comments (0)