gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:39:14 25 4
gpt4 key购买 nike

我有一个包含日期列的 HTML 表格。我想针对其中一个日期列为表格中的每一行实现倒计时。

这是我的 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>

这是我的 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>

计时器工作,但只为表格的最后一行填充最后一行的倒计时。

Result Screenshot

我已经检查过但无法弄清楚为什么它没有填充前面的行。

任何帮助将不胜感激。

最佳答案

我只需要在 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>

关于javascript - 为html表格中的每一行添加倒计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43328280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com