I made an OTP form verification and I put a resend code timer with a button that is disabled until the timer runs out. but when I submit a form and it returns an error, the timer start from the beginning and I don't want it. I want it to start from when it was.
var interval;
function countdown() {
clearInterval(interval);
interval = setInterval(function () {
var timer = $('.js-timer').html();
timer = timer.split(':');
var minutes = timer[0];
var seconds = timer[1];
seconds -= 1;
if (minutes < 0) return;
else if (seconds < 0 && minutes != 0) {
minutes -= 1;
seconds = 59;
}
else if (seconds < 10 && length.seconds != 2) seconds = '0' + seconds;
$('.js-timer').html(minutes + ':' + seconds);
if (minutes == 0 && seconds == 0) clearInterval(interval);
}, 1000);
}
function enableButton(id) {
document.getElementById("resendCode").disabled = true;
setTimeout(function () { document.getElementById("resendCode").disabled = false; }, 120000);
}
window.onload = function () {
$('.js-timer').text("2:00");
countdown();
enableButton();
};
function resendCodeBtn() {
$('.js-timer').text("2:00");
countdown();
enableButton();
};