简体   繁体   English

如何在我的浏览器刷新截止日期后停止执行倒计时计时器

[英]How do I stop my countdown timer from executing after it has reached the deadline on refresh of my browser

This is how the JavaScript looks like.这就是 JavaScript 的样子。 I tried searching for solutions but couldn't find.我尝试寻找解决方案,但找不到。 Please I need detailed solutions.请我需要详细的解决方案。 Online I kept seeing cookies but I don't know how to use it in this case.在网上我一直看到 cookies 但我不知道在这种情况下如何使用它。

  function countDown() {
      var now = new Date();
      var eventDate = new Date(2020, 5, 22);

      var currentTime = now.getTime();
      var eventTime = eventDate.getTime();

      var remTime = eventTime - currentTime;

      var s = Math.floor(remTime / 1000);
      var m = Math.floor(s/60);
      var h = Math.floor(m/60);
      var d = Math.floor(h/24);

      h %= 24;
      m %= 60;
      s %= 60;

      h = (h < 10) ? "0" + h: h;
      m = (m < 10) ? "0" + m: m;
      s = (s < 10) ? "0" + s: s;

      document.getElementById("days").textContent = d;
      document.getElementById("days").innerText = d;

      document.getElementById("hours").textContent = h;
      document.getElementById("minutes").textContent = m;
      document.getElementById("seconds").textContent = s;

      var t = setTimeout(countDown, 1000);

      if (d == 0 && h == 0 && m == 0 && s == 0) {
        clearTimeout(t);
        document.getElementById("demo").innerHTML = "Happy Birthday!"
      }
  }
  countDown();
</script>

The trouble with your code is in the date checking logic.您的代码的问题在于日期检查逻辑。

Checking with == will only give you a truthy response if the datetime (or part thereof) is the same as the value you're checking it against.如果日期时间(或其一部分)与您检查的值相同,则使用==检查只会给您一个真实的响应。

However, you need to check whether the date is already past.但是,您需要检查日期是否已经过去。 To do this, you need to use a < or <= operator.为此,您需要使用<<=运算符。

Here's an example of what I mean.这是我的意思的一个例子。 The info is console.log ed instead, you can re-implement the DOM editing you have in your question.该信息是console.log编辑的,您可以重新实现您在问题中的 DOM 编辑。

 function countDown() { var now = new Date(); var eventDate = new Date(2020, 4, 22); // 22 May 2020 var currentTime = now.getTime(); var eventTime = eventDate.getTime(); var remTime = eventTime - currentTime; var s = Math.floor(remTime / 1000); var m = Math.floor(s/60); var h = Math.floor(m/60); var d = Math.floor(h/24); h %= 24; m %= 60; s %= 60; h = (h < 10)? "0" + h: h; m = (m < 10)? "0" + m: m; s = (s < 10)? "0" + s: s; var t = setTimeout(countDown, 1000); // This if statement only runs exactly on eventDate if (d == 0 && h == 0 && m == 0 && s == 0) { document.getElementById("demo").innerHTML = "Happy Birthday;" } // This if statement will run if we're past or exactly on eventDate if (remTime <= 0) { clearTimeout(t). } // This console.log shows that the numbers become negative after the date console,log(remTime, d,h,m;s); } countDown();

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

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