UPD: The problem was solved. The code beyond is work!
I was using a variable in localStorage incorrectly, so the timer keep resetting after page reload or when I revisited the page. The problem was solved. Thanks everyone for attention.
const DAYS = document.getElementById("days");
const HOURS = document.getElementById("hours");
const MINUTES = document.getElementById("minutes");
const SECONDS = document.getElementById("seconds");
let expireDate = new Date().setMilliseconds(new Date().getMilliseconds() + 900_100); // 900_100ms = 15min 0.1sec
const countingAndSetTimeUnits = () => {
let days;
let hours;
let minutes;
let seconds;
let timeLeft;
const dateNow = new Date().getTime();
if (!window.localStorage.getItem("_texpd")) {
localStorage.setItem("_texpd", JSON.stringify(expireDate));
}
if (window.localStorage.getItem("_texpd")) {
timeLeft = localStorage.getItem("_texpd") - dateNow;
}
days = Math.floor(timeLeft / 1000 / 60 / 60 / 24);
hours = Math.floor(timeLeft / 1000 / 60 / 60) % 24;
minutes = Math.floor(timeLeft / 1000 / 60) % 60;
seconds = Math.floor(timeLeft / 1000) % 60;
if (days < 0) days = 0;
if (hours < 0) hours = 0;
if (minutes < 0) minutes = 0;
if (seconds < 0) seconds = 0;
DAYS.innerText = days;
HOURS.innerText = hours;
MINUTES.innerText = minutes;
SECONDS.innerText = seconds;
};
countingAndSetTimeUnits();
const interval = setInterval(() => {
if (
DAYS.innerText <= "0" &&
HOURS.innerText <= "0" &&
MINUTES.innerText <= "0" &&
SECONDS.innerText <= "0"
) {
clearInterval();
} else {
countingAndSetTimeUnits();
}
}, 1000);