Jquery multi-countdown .each() function

2019-03-03 18:40发布

I'm trying to make a multi-countdown on a page, which looks like :

<table>  
  <tr id="4236377487">
    <td class="remain"></td>
    <td>Something</td>
  </tr>
  <tr id="768769080">
    <td class="remain"></td>
    <td>Something else</td>
  </tr>
</table>

Countdown must be placed in :

<td class="remain"><!-- countdown --></td>

Each countdown starts with the row id value. Here's my code, but it doesn't work :

$(document).ready(function(){  
  $('.remain').each(function () {
     var count = $(this).attr("id");
     countdown = setInterval(function(){
     $(this).html(count + " seconds remaining!");
     if (count == 0) {
       //do something
     }
     count--; 
     }, 1000);
  });  
});

Thank for your help :)

Fabien

1条回答
Explosion°爆炸
2楼-- · 2019-03-03 19:30
$(document).ready(function(){  
  $('tr[id]').each(function () {
     var $this = $(this);
     var count = parseInt($this.attr("id"));
     countdown = setInterval(function(){
         $('.remain', $this).html(count + " seconds remaining!");
         if (count-- == 0) {
           //do something
           clearInterval(countdown);
         }
     }, 1000);
  });  
});

Try it here : http://jsfiddle.net/moeishaa/PwG45/

查看更多
登录 后发表回答