简体   繁体   English

页面重新加载后如何保持倒数计时器的当前值?

[英]How to keep the current values of the countdown timer after page reload?

UPD: The problem was solved. UPD:问题解决了。 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.我在 localStorage 中错误地使用了一个变量,因此在重新加载页面或重新访问页面后计时器会不断重置。 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);

Just with JS its not possible, you should use cookies仅使用 JS 是不可能的,您应该使用 cookies

With that functions, you will get your cookie, and if it exist retrieve and then save it.使用这些功能,您将获得您的 cookie,如果它存在,则检索并保存它。

const countingAndSetTimeUnits = () => {
  let days;
  let hours;
  let minutes;
  let seconds;

  let dateNow = new Date();
  
  if (getCookie("DAYS") != ""){
    dateNow.setDate(getCookie("DAYS"));
    dateNow.setHours(getCookie("HOURS"));
    dateNow.setMinutes(getCookie("MINUTES"));
    dateNow.setSeconds(getCookie("SECONDS"));
  }
  else{
    dateNow = new Date().getTime();
  }
  
  const timeLeft = expireDate - 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) daysContainer.classList.add(hidden);
  if (!hours) hoursContainer.classList.add(hidden);

  DAYS.innerText = days;
  HOURS.innerText = hours;
  MINUTES.innerText = minutes;
  SECONDS.innerText = seconds;


  setCookie("DAYS", "days", 1);
  setCookie("HOURS", "hours", 1);
  setCookie("MINUTES", "minutes", 1);
  setCookie("SECONDS", "seconds", 1);
};

function setCookie(cname, cvalue, exdays) {
  const d = new Date();
  d.setTime(d.getTime() + (exdays*24*60*60*1000));
  let expires = "expires="+ d.toUTCString();
  document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
  let name = cname + "=";
  let decodedCookie = decodeURIComponent(document.cookie);
  let ca = decodedCookie.split(';');
  for(let i = 0; i <ca.length; i++) {
    let c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}

I figured it out using localStorage.我使用 localStorage 解决了这个问题。 I added a check if the expireDate value exists in localStorage - use it for calculation;我添加了一个检查 localStorage 中是否存在 expireDate 值 - 使用它进行计算; if not, create it and use it for calculation.如果没有,请创建它并将其用于计算。 If you're interested, I've posted the final code in my question above.如果您有兴趣,我已经在上面的问题中发布了最终代码。 So you can use it to implement your own timer on the site.因此,您可以使用它在网站上实现您自己的计时器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM