1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| var TIME_LIMIT = 900;
var timePassed = 0;
var timeLeft = TIME_LIMIT;
var timerInterval = null;
function onTimesUp() {
clearInterval(timerInterval);
window.TIME_LIMIT = 900;
window.timeLeft = window.TIME_LIMIT;
window.timePassed = 0;
document.getElementById("otp_send").value = "Send";
document.getElementById("otp_send").removeAttribute('disabled');
}
function startTimer() {
timerInterval = setInterval(() => {
timePassed = timePassed += 1;
timeLeft = window.TIME_LIMIT - timePassed;
document.getElementById("otp_send").value = formatTime(timeLeft);
calculateTimeFraction();
if (timeLeft === 0) {
onTimesUp();
}
}, 1000);
}
function formatTime(time) {
const minutes = Math.floor(time / 60);
let seconds = time % 60;
if (seconds < 10) {
seconds = 0${seconds};
}
return ${minutes}:${seconds};
}
function calculateTimeFraction() {
const rawTimeFraction = timeLeft / window.TIME_LIMIT;
return rawTimeFraction - (1 / window.TIME_LIMIT) * (1 - rawTimeFraction);
} |
Partager