jquery计时器剩余写入时间

发布于 2024-11-29 07:27:50 字数 216 浏览 5 评论 0原文

我有一个段落字段,我想在其中放置此计时器剩余的秒数:

HTML:

<p>xx seconds remaining</p>

JavaScript:

window.setInterval(retrieveLog, 20000);

我该怎么做?

I have a paragraph field where I want to put the seconds remaining from this timer:

HTML:

<p>xx seconds remaining</p>

JavaScript:

window.setInterval(retrieveLog, 20000);

How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人心善变 2024-12-06 07:27:50

当您调用它时,存储结束时间:

var end = (new Date).getTime() + 20000 * 1000; // current time in ms since 1/1/1970, plus the interval time
window.setInterval(retrieveLog, 20000);

并添加另一个更新文本的间隔:

var interval = setInterval(function() {
    var now = (new Date).getTime();
    $("p").text(Math.floor(( end - now ) / 1000));
    if(now > end) clearInterval(interval);
}, 1000);

http://jsfiddle.net /pimvdb/b25KR/

When you call it, store the ending time:

var end = (new Date).getTime() + 20000 * 1000; // current time in ms since 1/1/1970, plus the interval time
window.setInterval(retrieveLog, 20000);

and add another interval that updates the text:

var interval = setInterval(function() {
    var now = (new Date).getTime();
    $("p").text(Math.floor(( end - now ) / 1000));
    if(now > end) clearInterval(interval);
}, 1000);

http://jsfiddle.net/pimvdb/b25KR/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文