JavaScript 计数器没有停止

发布于 2025-01-14 22:41:58 字数 1480 浏览 1 评论 0原文

对于我制作的这个计数器,我使用了 setInterval,当每个数字都等于零时,我希望这可以清除间隔,但它不起作用!有人可以帮我吗?

const counterHTML = document.querySelector("#counter");
const timeString = localStorage.getItem("Time");

function timeToAdd(stringTime) {
  const timeArray = stringTime.split(":");
  const hoursInMiliseconds = timeArray[0] * 60 * 60 * 1000;
  const minutesInMiliseconds = timeArray[1] * 60 * 1000;
  const TIME_IN_MILISECONDS = hoursInMiliseconds + minutesInMiliseconds;
  return TIME_IN_MILISECONDS;
}

const actualTime = Date.now();
const finalTimeDateObj = new Date(actualTime + timeToAdd(timeString));
const finalTime = finalTimeDateObj.getTime();

const time = (myTimerInterval) => {
  const timeNow = Date.now();
  const diference = finalTime - timeNow;
  let displayTimeArray = msToTime(diference);
  console.log(diference);
  const hours = String(displayTimeArray[0]).padStart(2, "0");
  const minutes = String(displayTimeArray[1]).padStart(2, "0");
  const seconds = String(displayTimeArray[2]).padStart(2, "0");
  if (hours == "00" && minutes == "00" && seconds == "00") {
    clearInterval(myTimerInterval);
  }
  counterHTML.innerText = `${hours}:${minutes}:${seconds}`;
};

function msToTime(millis) {
  let h, m, s;
  h = Math.floor(millis / 1000 / 60 / 60);
  m = Math.floor((millis / 1000 / 60 / 60 - h) * 60);
  s = Math.floor(((millis / 1000 / 60 / 60 - h) * 60 - m) * 60);
  return [h, m, s];
}

var myTimerInterval = setInterval(time, 1000);

for this counter I made, I used the setInterval, and when every digit is equal to zero I expect this to clear the interval, but it's not working! Can someone help me out?

const counterHTML = document.querySelector("#counter");
const timeString = localStorage.getItem("Time");

function timeToAdd(stringTime) {
  const timeArray = stringTime.split(":");
  const hoursInMiliseconds = timeArray[0] * 60 * 60 * 1000;
  const minutesInMiliseconds = timeArray[1] * 60 * 1000;
  const TIME_IN_MILISECONDS = hoursInMiliseconds + minutesInMiliseconds;
  return TIME_IN_MILISECONDS;
}

const actualTime = Date.now();
const finalTimeDateObj = new Date(actualTime + timeToAdd(timeString));
const finalTime = finalTimeDateObj.getTime();

const time = (myTimerInterval) => {
  const timeNow = Date.now();
  const diference = finalTime - timeNow;
  let displayTimeArray = msToTime(diference);
  console.log(diference);
  const hours = String(displayTimeArray[0]).padStart(2, "0");
  const minutes = String(displayTimeArray[1]).padStart(2, "0");
  const seconds = String(displayTimeArray[2]).padStart(2, "0");
  if (hours == "00" && minutes == "00" && seconds == "00") {
    clearInterval(myTimerInterval);
  }
  counterHTML.innerText = `${hours}:${minutes}:${seconds}`;
};

function msToTime(millis) {
  let h, m, s;
  h = Math.floor(millis / 1000 / 60 / 60);
  m = Math.floor((millis / 1000 / 60 / 60 - h) * 60);
  s = Math.floor(((millis / 1000 / 60 / 60 - h) * 60 - m) * 60);
  return [h, m, s];
}

var myTimerInterval = setInterval(time, 1000);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清晰传感 2025-01-21 22:41:58

首先我看到这个问题

const time = (myTimerInterval) => { 
   ...
   if (hours == "00" && minutes == "00" && seconds == "00") {
        // myTimerInterval === undefined
        clearInterval(myTimerInterval);
   }
   ...
}

setInterval 调用的函数没有收到任何参数。 myTimerInterval 在这种情况下是未定义的,因此clearInterval 不起作用。

尝试删除该参数

const time = () => { 
   ...
   // now myTimerInterval references the global var
   if (hours == "00" && minutes == "00" && seconds == "00") {
        clearInterval(myTimerInterval);
   }
   ...
}

First of all I see this problem

const time = (myTimerInterval) => { 
   ...
   if (hours == "00" && minutes == "00" && seconds == "00") {
        // myTimerInterval === undefined
        clearInterval(myTimerInterval);
   }
   ...
}

The function that setInterval calls doesn't receipt any arg. myTimerInterval in this context is undefined, so clearInterval doesn't work.

Try removing that arg

const time = () => { 
   ...
   // now myTimerInterval references the global var
   if (hours == "00" && minutes == "00" && seconds == "00") {
        clearInterval(myTimerInterval);
   }
   ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文