Jquery多次倒计时.each()函数

时间:2011-03-19 07:33:44

标签: jquery countdown each

我正在尝试在页面上进行多次倒计时,如下所示:

<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>

倒计时必须放在:

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

每次倒计时都以行ID值开头。这是我的代码,但它不起作用:

$(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);
  });  
});

感谢您的帮助:)

法比安

1 个答案:

答案 0 :(得分:4)

$(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);
  });  
});

在此处试试:http://jsfiddle.net/moeishaa/PwG45/