I have multiple countdowns on my page on page load. I want to make it all work with single javascript function. I have tried many ways to make it work but any of them worked for me.
I have found some similar questions here but none of them helped me.
What am I doing wrong? What should I do?
http://jsfiddle.net/d3g840pe/1/
HTML:
<table>
<tr id="id1">
<td>img</td>
<td>#</td>
<td>type</td>
<td>status</td>
<td>
<span class="counter" rel='120'></span>
</td>
</tr>
<tr id="id2">
<td>img</td>
<td>#</td>
<td>type</td>
<td>status</td>
<td>
<span class="counter" rel='200'></span>
</td>
</tr>
<tr id="id3">
<td>img</td>
<td>#</td>
<td>type</td>
<td>status</td>
<td>
<span class="counter" rel='50'></span>
</td>
</tr>
</table>
JS:
$(function() {
$tr = $('.counter').closest('tr');
$trId = $tr.attr('id');
$span = $('#' + $trId).find('.counter');
$counter = $span.attr('rel');
setInterval(function() {
$counter--;
$span.attr('rel', $counter);
if ($counter >= 0) {
$span.html($counter);
}
// Display '$counter' wherever you want to display it.
if ($counter === 0) {
//return true;
console.log('finished');
clearInterval($counter);
}
}, 1000);
});