简体   繁体   English

为每个表格行JS设置一个计时器

[英]Setting a timer for each table row JS

I have a problem in setting a decreasing timer for the table rows in my code! 我在为代码中的表行设置递减计时器时遇到问题!

I've tried multiple solution but it's just not working 我已经尝试了多种解决方案,但没有用

 window.onload = function() { var table = document.getElementById("bidsTable"); var x = setInterval(function() { for (var i = 1, row; row = table.rows[i]; i++) { var endDate = row.cells[4]; countDownDate = new Date(endDate.innerHTML.replace(/-/g, "/")).getTime(); var countDown = row.cells[5]; var now = new Date().getTime(); var t = countDownDate - now; var days = Math.floor(t / (1000 * 60 * 60 * 24)); var hours = Math.floor((t%(1000 * 60 * 60 * 24))/(1000 * 60 * 60)); var minutes = Math.floor((t % (1000 * 60 * 60)) / (1000 * 60)); var seconds = Math.floor((t % (1000 * 60)) / 1000); countDown.innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s "; if (t < 0) { clearInterval(x); } } }, 1000);} //1000 show the code every second 
 <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 >2020-04-10 3:48:47 PM</td> <td >1</td> <td >2020-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 id="demo" ></td> </tr> </tbody> </table> 

how can I make the counter decrease every second? 如何使计数器每秒减少一次? it only updates when I refresh the page 它仅在刷新页面时更新

I guess it has to do with the loop or the setInterval method! 我想这与循环或setInterval方法有关!

in your code you defined countDownDate in a for loop for all three rows then you define t = countDownDate - now which makes ta negative number because when for loop ends and javascript starts to to run this part of the code the value of countDownDate represents time of Five hectare farming land for lease which happened before now so t = countDownDate - now becomes a negative number then you check wether t is a negative number or not here 在您的代码中,在for循环中为所有三行定义了countDownDate ,然后定义了t = countDownDate - now ,它使ta为负数,因为当for循环结束并且javascript开始运行此部分代码时, countDownDate的值表示时间之前发生过的五公顷农地出租 ,所以t = countDownDate - now变为负数,然后检查此处是否为负数

if (t < 0) { clearInterval(x); }

and because t is a negative number you just stop the interval and it only runs once 并且因为t是负数,所以您只停止间隔并且它只运行一次

if you want to show 0 days 0 minutes . 如果要显示0天0分钟。 . . for times that has passed i've modified your code as below : 对于已经过去的时间,我已经修改了您的代码,如下所示:

  window.onload = function() { var table = document.getElementById("bidsTable"); var x = setInterval(function() { var now = new Date().getTime(); for (var i = 1, row; row = table.rows[i]; i++) { var endDate = row.cells[4]; countDownDate = new Date(endDate.innerHTML.replace(/-/g, "/")).getTime(); var countDown = row.cells[5]; var t = countDownDate - now; var days = Math.floor(t / (1000 * 60 * 60 * 24)); var hours = Math.floor((t%(1000 * 60 * 60 * 24))/(1000 * 60 * 60)); var minutes = Math.floor((t % (1000 * 60 * 60)) / (1000 * 60)); var seconds = Math.floor((t % (1000 * 60)) / 1000); if(t<0){ days = 0; hours = 0; minutes = 0; seconds = 0; } countDown.innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s "; } }, 1000);} //1000 show the code every second 
 <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 >2020-04-10 3:48:47 PM</td> <td >1</td> <td >2020-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 id="demo" ></td> </tr> </tbody> </table> 

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

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