简体   繁体   English

半小时倒数

[英]Half Hour Countdown

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. 例如,如果是10:05,我希望它显示倒计时还有25分钟。 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. **编辑:假设他们在9:55加载页面。 I would like the countdown to display 5 mintues remaining. 我想倒数显示剩余的5分钟。 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. 然后,当10:00到来时,我希望页面刷新,以便倒计时可以再次显示30分钟。

Here's a function that does that: 这是执行此操作的函数:

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

That's JavaScript, so there's no server involved. 那是JavaScript,因此不涉及服务器。 To display a continuous countdown, you could do this (assuming you have an element with an ID of "countdown"): 要显示连续倒数,可以执行以下操作(假设您有一个ID为“ 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);

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

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