简体   繁体   English

为html表中的每一行添加倒计时

[英]add countdown for each row in html table

I have an HTML table that contains a date column.我有一个包含日期列的 HTML 表。 I want to implement a countdown for each row in the table against one of the date columns.我想针对日期列之一为表中的每一行实施倒计时。

Here goes my HTML code:这是我的 HTML 代码:

 <table  style="width: 100%" id="bidsTable">
                <thead>
                    <tr>
                        <th>Title</th>
                        <th >Amount</th>

                        <th >Start Date</th>
                        <th >Bids</th>
                        <th >End Date</th>
                        <th ></th>
                    </tr>
                </thead>
                <tbody>
                        <tr>
                            <td >Peugeot 406 car fro sale</td>
                            <td >800000.00</td>

                            <td >2017-04-10 3:48:47 PM</td>
                            <td >1</td>
                            <td >2017-05-10 3:48:47 PM</td>
                            <td ></td>
                        </tr>
                        <tr>
                            <td >House for sale in Kubwa</td>
                            <td >4000000.00</td>

                            <td >2017-04-10 3:48:47 PM</td>
                            <td >0</td>
                            <td >2017-06-10 3:48:47 PM</td>
                            <td ></td>
                        </tr>
                        <tr>
                            <td >Five hectare farming land for lease</td>
                            <td >3000000.00</td>

                            <td >2017-04-10 3:48:47 PM</td>
                            <td >0</td>
                            <td >2017-07-10 3:48:47 PM</td>
                            <td ></td>
                        </tr>



                </tbody>
            </table>

And here goes my javascript code这是我的 javascript 代码

 <script>
var table = document.getElementById("bidsTable");
for (var i = 1, row; row = table.rows[i]; i++) {
    //iterate through rows
    //rows would be accessed using the "row" variable assigned in the for loop

    var endDate = row.cells[4];
    countDownDate = new Date(endDate.innerHTML.replace(/-/g, "/")).getTime();
    var countDown = row.cells[5];
    // 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 an 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
        countDown.innerHTML = (days + "d " + hours + "h "
            + minutes + "m " + seconds + "s ");

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

The timer works but only populates the countdown for the last row for the last row of the table.计时器工作,但只为表格的最后一行填充最后一行的倒计时。

结果截图

I've checked but can't figure out why it's not populating the previous rows.我已经检查过但无法弄清楚为什么它没有填充前一行。

Any help will be appreciated.任何帮助将不胜感激。

I just had to run the loop inside the setInterval function and not the setInterval function inside the loop.我只需要在setInterval函数内运行循环,而不是在循环内运行setInterval函数。

<script>

var table = document.getElementById("bidsTable");

var x = setInterval(
    function () {

        for (var i = 1, row; row = table.rows[i]; i++) {
            //iterate through rows
            //rows would be accessed using the "row" variable assigned in the for loop

            var endDate = row.cells[4];
            countDownDate = new Date(endDate.innerHTML.replace(/-/g, "/")).getTime();
            var countDown = row.cells[5];
            // Update the count down every 1 second

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

            // Find the distance between now an 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
            countDown.innerHTML = (days + "d " + hours + "h "
                + minutes + "m " + seconds + "s ");

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

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

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