duanou3868 2017-12-25 16:51
浏览 55

如何为多个元素制作倒计时时钟?

Below is my php code which connects to my database and collects information and echo's it out as a html table. But the only problem is all the count down clocks count down to a single date. Please help me so that each clock counts down towards an individuals graduation date.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "graduation";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id, name, date, status FROM student";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th><th>Date of Graduation</th><th>Count-Down-Clock</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
    echo "<tr><td>".$row["id"]."</td><td>".$row["name"]."</td><td>".$row["date"]."</td><th><p class='demo'></p></th></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>

Below is my javascript code which makes

into a count down clock.
<script>



// Set the date we're counting down to
var countDownDate = new Date("Sep 5, 2018 15:37:25").getTime();

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

// Get todays date and time
var now = new Date().getTime();

// Find the dif between now an the count down date
var dif = countDownDate - now;

// Time calculations for days, hours, minutes and seconds
 var d = Math.floor(dif / (1000 * 60 * 60 * 24));
 var h = Math.floor((dif % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
 var m = Math.floor((dif % (1000 * 60 * 60)) / (1000 * 60));
 var s = Math.floor((dif % (1000 * 60)) / 1000);

 var formatted = d + "d " + h + "h " + m + "m " + s + "s ";
// Output the result in an element with id="demo"
    [...document.querySelectorAll(".demo")].forEach(el => el.innerHTML = dif < 0 ? "Expired" : formatted);




    // If the count down is over, write some text 
    if (dif < 0) {
    clearInterval(x);

    }
}, 1000);
</script>
  • 写回答

1条回答 默认 最新

  • doushao1087 2017-12-25 17:55
    关注

    Problems:

    • var countDownDate = new Date("Sep 5, 2018 15:37:25").getTime(); is hardcoded
    • The same HTML result text is used for all .demo elements

    Solution:

    In the code where you have <td>".$row["date"]."</td> place instead:

    <td>
        <div data-date='". $row["date"] ."'></div>
    </td>
    

    JS should than read forEach element [data-date] it's own data-date value and use it as countdown start; instead of the currently hardcoded new Date("Sep 5, 2018 15:37:25").getTime();

    Here's an example with the modified JS:

    function countDown(el) { // Create a function that recieves the element as argument
    
      var countDownDate = new Date(el.dataset.date).getTime();
    
      var x = setInterval(function() {
    
        var now = new Date().getTime();
        var dif = countDownDate - now;
        var d = Math.floor(dif / (1000 * 60 * 60 * 24));
        var h = Math.floor((dif % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        var m = Math.floor((dif % (1000 * 60 * 60)) / (1000 * 60));
        var s = Math.floor((dif % (1000 * 60)) / 1000);
    
        var formatted = d + "d " + h + "h " + m + "m " + s + "s ";
        // Output the result in the argument element
        el.innerHTML = dif < 0 ? "Expired" : formatted;
    
        // If the count down is over, stop Intervals
        if (dif < 0) {
          clearInterval(x);
        }
      }, 1000);
    
    }
    
    // Init countDown!
    [...document.querySelectorAll("[data-date]")].forEach(el => countDown(el));
    <table>
      <tr>
        <td>
          John
        </td>
        <td>
          <div data-date='Oct 20, 2018 12:00:00'></div>
        </td>
      </tr>
      <tr>
        <td>
          Peter
        </td>
        <td>
          <div data-date='Sep 5, 2018 15:37:25'></div>
        </td>
      </tr>
    </table>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计