Jquery多次倒计时

时间:2011-08-06 16:06:13

标签: php jquery countdown

我找到了这个脚本

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

以及HTML代码:

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

结果是这样的:

4236375205 seconds remaining!   Something
768766798 seconds remaining!    Something else

我的日期格式为2011-10-29 10:05:00,我使用<?php echo $date ?>显示此日期

我需要的是将$ date传递给<tr id=所以剩下的时间显示在那个日期,但保持相同的格式Ymd H:i:s甚至H:i:s会很棒。

我刚刚在http://keith-wood.name/countdown.html检查了jQuery Countdown插件,但由于我需要多次倒计时,它不适合我。

我希望这很清楚,你可以理解我的问题。 提前致谢并抱歉我的英文

1 个答案:

答案 0 :(得分:0)

试试这个

$(document).ready(function(){  
  $('tr[id]').each(function () {
     var $this = $(this);
     var count = parseInt($this.attr("id"));
     countdown = setInterval(function(){
         var curDate = (new Date()).getMilliseconds();
         var newDate = new Date(count-curDate);
         var dateString = newDate.getHours() + ":" + newDate.getMinutes() + ":" + newDate.getSeconds();
         $('.remain', $this).html(dateString + " seconds remaining!");
         if (count-- == 0) {
           //do something
           clearInterval(countdown);
         }
     }, 1000);
  });  
});