繁体   English   中英

如何在 HTML 代码中显示倒数计时器脚本的 output

[英]How do I display the output of a countdown timer script in HTML code

愚蠢,但我相信有一个简单的答案。 我试图弄清楚如何显示我在另一个脚本中在线找到的这个计时器脚本的 output。 这是我在下面找到的计时器脚本

<!-- 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, 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);

  // 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>

我希望能够在我放“我想在这里显示计时器”的地方显示计时器

  <div class="flash-infos">   
    <div class="flash-info">
      <font color="white"; font size = 2; ><b>"I want to display the timer here"</b></font> <center>
    </div>    
  </div>

答案是:

<b id="memo"></b>

好吧,我假设您在“p”标签和“font”标签中使用了 id="demo"。 所以前者优先。 只需在此处保留 id="demo" :

 <div class="flash-infos">   
    <div class="flash-info">
      <font font size = 2; ><b id="demo"></b></font> <center>
    </div>    
  </div>

并删除您在 HTML 的其他部分中使用的任何其他 id="demo"。 请注意,我已删除 color="white",不确定它是否与默认背景颜色同步。 如果可能是白色,请尝试设置不同的颜色。

暂无
暂无

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

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