首页 > 解决方案 > 创建自定义倒数计时器

问题描述

我是一名平面设计师,还负责为我工作的教堂建立一个网站。我过去建立了很多网站,所以这根本不是问题,但是,我希望对这个网站做的事情是拥有尽可能多的元素,这些元素可以简单地单独运行。当谈到 JavaScript 时,我是一个完全的初学者,老实说,我对我在做什么完全没有概念。

我希望新站点有一个自定义倒计时计时器,该计时器将倒计时到我们上线的时间,然后在设定的时间段内显示我们的直播链接,然后重置。下面的代码显示了我已经设法通过在互联网上搜索而编写的内容。它的问题在于它依赖于一个人(其中一半时间是网络志愿者)记住在当前服务完成后将日期和时间更改为下一个服务,而 90% 的时间他们忘记了.

在一个完美的世界里,我希望这个倒计时计时器在下午 6:00 自动倒计时到最近的星期二,然后显示指向我们直播的链接 20 分钟,然后重置。或者,如果周日上午 10:00 先到,则倒计时到周日上午 10 点,然后显示我们的直播链接 1 小时 15 分钟,然后重置。从理论上讲,我想这可以无限期地运行,因为它只会检查周二下午 6 点是否先到,或者周日上午 10 点先到,然后倒计时到这些时间,暂停,显示一些超链接文本,然后重复。

如果这里已经讨论过,我很抱歉,我尝试尽可能彻底地搜索并且找不到任何东西。

任何帮助都将不胜感激。

<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<p id="demo" class="countdown-live" style="text-align:center;"></p>

<script>
// Set the date we're counting down to
var countDownDate = new Date("October 20, 2020 18:00:00").getTime();

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

  // Get todays 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);
    
  // Output the result in an element with id="demo"
  document.getElementById("demo").innerHTML = days + " d " + hours + " h "
  + minutes + " m & " + seconds + " s";
    
  // If the count down is over, write some text 
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = '<a href="https://newhopechurchtn.org/new-hope-online">Watch Live!</a>';
  }
}, 1000);
</script>

</body>
</html>

标签: javascripttimercountdowntimer

解决方案


JAVASCRIPT CSS HTML 代码:

<script>
// Set the date we're counting down to
// you can set according your own need
var countDownDate = new Date("Jan 1, 2022 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);
    
  // Output the result in an element with id="demo"
  document.getElementById("demo").innerHTML = days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";
    
  // If the count down is over, write some text 
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "Wait Is Over";
  }
}, 1000);
</script>


CSS part
<style>
p {
  text-align: center;
  font-size: 60px;
  margin-top: 0px;
}
</style>


HTML part
<p id="demo"></p>


推荐阅读