javascript - 使用从数据库检索的数据显示多个实时倒计时

标签 javascript countdown

我有多个倒计时需要显示在一页上,剩余时间是来自数据库的数据。

我正在使用另一个网站的示例,并且尝试了很多,但只能显示一个。

我的代码:

 <?
      $result = mysql_query("SELECT * FROM `time` ");
      while ($row = mysql_fetch_array($result)) {
           $futuredate = date('F d, Y H:i:s', time()+$row['TimeLeft']);
      
 ?>

 <div id="cdcontainer"></div>
 <script type="text/javascript">
      var launchdate = new cdLocalTime("cdcontainer", "server-php", 0, "<?php echo $futuredate;?>", "debugmode")
      launchdate.displaycountdown("days", formatresults)
 </script>

 <?php

  }
 ?>

最佳答案

我不熟悉 cdLocalTime,但我在代码中看到的一个错误是您添加了两个具有相同 id 的 div。这并不漂亮,但请尝试:

$i = 0;
while ($row = mysql_fetch_array($result)) {
    $futuredate = date('F d, Y H:i:s', time()+$row['TimeLeft']);
?>
<div id="cdcontainer_<?=$i?>"></div>
<script type="text/javascript">
var launchdate=new cdLocalTime("cdcontainer_<?=$i++?>", "server-php", 0, "<?= $futuredate;?>", "debugmode")
launchdate.displaycountdown("days", formatresults)
</script>

<?php }

因此,这两次您将拥有 cdcontainer_0 和 cdcontainer_1。以防万一“

关于javascript - 使用从数据库检索的数据显示多个实时倒计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8610809/

相关文章:

javascript - 类型错误 : undefined is not a function --- Need to troubleshoot

javascript - 我可以使用 TypeScript 在 axios.get 方法中的泛型类型中指定任何类型

javascript - 每行数据表中的倒数计时器

javascript - 未捕获的类型错误 : Cannot read property 'innerHTML' of null

javascript - XSL 中的空元素 - 嵌套问题

javascript - JS - 对象包含数组名称

python将时间添加到已经运行的倒计时

javascript - 在 Canvas 中使用 JS 倒计时器和 requestAnimationFrame

javascript - 在特定时间重置倒计时

javascript - javascript中的SetInterval重复显示而不是改变