简体   繁体   English

单击按钮后显示倒数计时器

[英]Show countdown timer after button click

I have a table with many lines (about 50 - 100 at the end) and each lines have a different timeout. 我的表有很多行(末尾大约有50-100),每行都有不同的超时时间。 I want when i click on the first button (link) the countdown of the first line start, if on the second line, the second countdown start. 我想在单击第一个按钮(链接)时开始第一行的倒计时,如果在第二行上,则第二次倒计时开始。

After, when the countdown is finish, any word will display instead of the timer. 之后,当倒计时完成时,将显示任何字词而不是计时器。

Here are two exmeple of lines: 这是两行示例:

<tr>
      <td><form action="http://www.google.fr"><input type="submit" formtarget="_blank" value="Google 3 seconds"></form>
      </td>
      <td>00:00:03</td>
</tr>
<tr>
      <td><form action="http://www.google.fr"><input type="submit" formtarget="_blank" value="Google 2h30mins"></form>
      </td>
      <td>02:30:00</td>
</tr>

For js i found this here: http://jsfiddle.net/6nDYd/10/ 对于js,我在这里找到了它: http : //jsfiddle.net/6nDYd/10/

Anyone can help me to create the js script with the link i provide ? 任何人都可以通过我提供的链接帮助我创建js脚本吗?

Try this: 尝试这个:

Ref from SO 来自SO的参考

 function toTimeString(seconds) { return (new Date(seconds * 1000)).toUTCString().match(/(\\d\\d:\\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) { alert('sorry, out of time'); clearInterval(seconds); } }, 1000); } $('.btn').on('click', startTimer); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td> <form action="http://www.google.fr"> <input class="btn" type="button" formtarget="_blank" value="Google 3 seconds"> </form> </td> <td>00:00:03</td> </tr> <tr> <td> <form action="http://www.google.fr"> <input class="btn" type="button" formtarget="_blank" value="Google 2h30mins"> </form> </td> <td>02:30:00</td> </tr> </table> 

How to continue timer after browser is refreshed and closed? 浏览器刷新和关闭后如何继续计时?

 function toTimeString(seconds) { return (new Date(seconds * 1000)).toUTCString().match(/(\\d\\d:\\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) { alert('sorry, out of time'); clearInterval(seconds); } }, 1000); } $('.btn').on('click', startTimer); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td> <form action="http://www.google.fr"> <input class="btn" type="button" formtarget="_blank" value="Google 3 seconds"> </form> </td> <td>00:00:03</td> </tr> <tr> <td> <form action="http://www.google.fr"> <input class="btn" type="button" formtarget="_blank" value="Google 2h30mins"> </form> </td> <td>02:30:00</td> </tr> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM