重置计时器

时间:2017-05-23 10:45:30

标签: javascript jquery

我有一个倒数计时器,用于在0秒点击一个按钮,它可以工作,但我希望时间本身在点击后重置。使用此代码,我希望在点击Time 1时重置work2

function toTimeString(seconds) {
  return (new Date(seconds * 1000)).toUTCString().match(/(:\d\d:\d\d)/)[0];
}

function startTimer() {
  var nextElem = $(this).parents('td').next();
  var duration = nextElem.text();
  var a = duration.split(':');
  var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
  setInterval(function() {
    seconds--;
    if (seconds >= 0) {
      nextElem.html(toTimeString(seconds));
    }
    if (seconds === 0) {
      document.getElementById('work2').click();
      clearInterval(seconds);
    }
  }, 1000);
}
$('.lazy').on('click', startTimer);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
 
        
<tr>
<td>
<input id="work1" class="lazy" type="button"  value="Time 1"/>
</td>
<td>:00:05</td>
</tr>


 <tr>
 <td>
 <input id="work2" class="lazy" type="button" value="Time 2" type="button" />
 </td>
 <td>:10:00</td>
 </tr>
</table>

1 个答案:

答案 0 :(得分:1)

计时器的用法如下:启动计时器并将计时器句柄存储在变量中。停止计时器时,您将移交此变量。

var timer1 = setInterval(function(){}, 1000);
..
clearInterval(timer1);
相关问题