JSFiddle - React, Tailwind, and code Playground
HTML
<div id="timerText">Timer</div>
JavaScript
var timerElement = $('#timerText');
function timerCreate(minutes, seconds) {
var finalTimerTime = getFinalTime(minutes, seconds);
// starts run timer every second and gets handler to the timer, so it can be stopped when time gets
// final time
var timer = setInterval(function () {
var currentTime = new Date();
if (currentTime > finalTimerTime) {
clearInterval(timer);
console.log('Done');
} else {
printTimer(new Date(finalTimerTime - currentTime));
}
}, 1000);
printTimer(new Date(finalTimerTime - new Date()));
}
// prints timer valud
function printTimer(finalTimerTime) {
timerElement.text(finalTimerTime.getMinutes() + ' : ' + finalTimerTime.getSeconds());
}
// gets final time of the timer
function getFinalTime(minutes, seconds) {
var finalTimerTime = new Date();
finalTimerTime.setMinutes(finalTimerTime.getMinutes() + minutes);
finalTimerTime.setSeconds(finalTimerTime.getSeconds() + seconds);
return finalTimerTime;
}
// starts timer
$(function () {
timerCreate(5, 4);
});