I have attempted using the for loop to display my Countdown, but I don't know where I am getting it wrong. Is there a simpler way to display the countdown for each row using vanilla javascript?
My Table Below:
<html>
<body>
<table style="width:50%" id= "tab">
<tr>
<th>Name</th>
<th>Exp</th>
<th>Count Down</th>
</tr>
<tr>
<td> A</td>
<td class = 'exp1'> 09/08/2019</td>
<td class = 'demo'> </td>
</tr>
<tr>
<td> B</td>
<td class = 'exp1'> 09/08/2020</td>
<td class = 'demo'> </td>
</tr>
</table>
</body>
</html>
Below is my javascript code:
<script>
var exp = document.getElementById('tab').getElementsByClassName('exp1');
var x = document.getElementById('tab').getElementsByClassName('demo');
var now = new Date();
function test(){
for (i = 0; i<exp.length; i++){
var e = new Date( exp[i].innerHTML);
var timeDiff = e.getTime() - now.getTime();
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %=24;
minutes %=60;
seconds %=60;
var timer = setTimeout('test()', 1000);
}
for (i=0; i<x.length; i++){
x[i].innerHTML = days + " " + hours + " " + minutes + " " + seconds;
}
}
</script>