半小时倒计时

发布于 2024-11-02 06:42:38 字数 227 浏览 1 评论 0原文

我如何为用户设置一个倒计时时间,让用户看到每半小时倒计时一次。例如,如果是 10:05,我希望它显示还剩 25 分钟的倒计时。我想它需要耗尽服务器时间。感谢您的帮助!

另外,是否可以在倒计时结束时刷新页面,以便用户可以在接下来的半小时内看到新页面?

**编辑:假设他们在 9:55 加载页面。我希望倒计时显示还剩 5 分钟。然后,一旦 10:00 到来,我希望页面刷新,以便倒计时可以再次显示 30 分钟。

How would I make a countdown time for users to see that would countdown to every half hour. For example, if it were 10:05, I would want it to display a countdown that had 25 minutes remaining. I guess it would need to run off of the server time. Thanks for your help!

Also, would it be possible to refresh the page at the end of the countdown so the user could see the new one for the next half hour?

**Edit: Say they load the page at 9:55. I would like the countdown to display 5 mintues remaining. Then as soon as 10:00 were to come, I would like the page to refresh so that the countdown could display 30 minutes again.

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

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

发布评论

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

评论(1

终陌 2024-11-09 06:42:38

这是一个执行此操作的函数:

function numMinutes() {
     return 30 - new Date().getMinutes() % 30;
}

这​​是 JavaScript,因此不涉及服务器。要显示连续倒计时,您可以这样做(假设您有一个 ID 为“倒计时”的元素):

setInterval(function() {
     var el = document.getElementById("countdown");
     while(el.childNodes.length > 0) el.removeChild(el.firstChild);
     el.appendChild(document.createTextNode(numMinutes().toString() + ' minutes left!'));
}, 1000);

或者,用(非标准)简写:

setInterval(function() {
     document.getElementById("countdown").innerHTML = numMinutes().toString() + ' minutes left!';
}, 1000);

Here's a function that does that:

function numMinutes() {
     return 30 - new Date().getMinutes() % 30;
}

That's JavaScript, so there's no server involved. To display a continuous countdown, you could do this (assuming you have an element with an ID of "countdown"):

setInterval(function() {
     var el = document.getElementById("countdown");
     while(el.childNodes.length > 0) el.removeChild(el.firstChild);
     el.appendChild(document.createTextNode(numMinutes().toString() + ' minutes left!'));
}, 1000);

Or, in (non-standard) shorthand:

setInterval(function() {
     document.getElementById("countdown").innerHTML = numMinutes().toString() + ' minutes left!';
}, 1000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文