javascript - 倒数计时器 10 分钟后消失且不重置计数器

标签 javascript html

我正在开发一个 10 分钟的倒计时器。我想知道如何让计数器在时间到时停止显示,或者即使在刷新页面后仍从剩余时间开始计数。

// 10 minutes from now
var time_in_minutes = 10;
var current_time = Date.parse(new Date());
var deadline = new Date(current_time + time_in_minutes * 60 * 1000);


function time_remaining(endtime) {
  var t = Date.parse(endtime) - Date.parse(new Date());
  var seconds = Math.floor((t / 1000) % 60);
  var minutes = Math.floor((t / 1000 / 60) % 60);
  var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
  var days = Math.floor(t / (1000 * 60 * 60 * 24));
  return {
    'total': t,
    'days': days,
    'hours': hours,
    'minutes': minutes,
    'seconds': seconds
  };
}

function run_clock(id, endtime) {
  var clock = document.getElementById(id);

  function update_clock() {
    var t = time_remaining(endtime);
    clock.innerHTML = "<span style='color: #E89E88; font-weight: bold;'>DÉSE PRISA! </span>" + 'Use el código de descuento' + "<span style='font-weight: bold; color:white;'>'DESCUENTO5'</span>" + ' en los próximos ' + t.minutes + ' m ' + t.seconds + ' s ' + ', y ahorre un extra 5% en su pedido!';
    if (t.total == 0) {
      clearInterval(timeinterval);
    }
  }

  update_clock(); // run function once at first to avoid delay
  var timeinterval = setInterval(update_clock, 1000);
}
run_clock('clockdiv', deadline);
#clockdiv {
  background: #bfbfbf;
  color: white;
  padding: 6px;
  border-radius: 4px;
  font-size: 16px;
  margin-bottom: 16px;
}
<div id="clockdiv"></div>

最佳答案

尝试此代码,如果您重新加载页面或在新窗口中打开它,您可以设置倒计时的日期

    <!-- Display the countdown timer in an element -->
<p id="demo"></p>

<script>
// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2021 15:37:25").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

  // Get today's date and time
  var now = new Date().getTime();

  // Find the distance between now and the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Display the result in the element with id="demo"
  document.getElementById("demo").innerHTML = days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";

  // If the count down is finished, write some text
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "EXPIRED";
  }
}, 1000);
</script>

关于javascript - 倒数计时器 10 分钟后消失且不重置计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61583417/

相关文章:

javascript - 隐藏一个容器并显示另一个

javascript - HTML Javascript |如何获取链接数组的值

javascript - 将表单结果从 php 文件重定向到另一个文件

javascript - 将对象作为参数传递给指令

javascript - Angular PWA - 为什么浏览器缓存不断增长?如何阻止它?

javascript - 使用 JS 切换显示/隐藏

javascript - 如何确定实际显示的 CSS3 列数(相对于指定的列数)?

javascript - 当模态在 w3.css 中滚动时如何隐藏文档垂直滚动条?

javascript - 谷歌图表 : Y-Axis numbers not visible

javascript - Aurelia 中的动态路由