jQuery计时器/倒计时(使用服务器时间) [英] jQuery timer/countdown using server time
问题描述
我正在尝试每10分钟进行一次倒计时(根据服务器时间,而不是客户端时间).每10分钟(例如10 am、10:10am、10:20:am [数十个])需要刷新页面并重新开始.
I'm trying to have a countdown for every 10 minutes (according to the server time, not client). Every 10 minutes (eg. 10am, 10:10am, 10:20:am [on the tens]) it needs to refresh the page and start over.
我发现了超时功能,但是没有一个使用服务器时间,并且可以重设数十个时间.
I've found timeout functions but none that use the server time and that I can have reset on the tens.
以前有人做过这样的事吗?
Has anyone done anything like this before?
谢谢
推荐答案
我最终在评论/答案和其他帖子中使用了一些想法,并提出了这个想法.
I ended up using some of the ideas in comments/answers and other posts and came up with this.
它每10分钟倒计时一次(再次,取决于服务器),并显示计时器值.到达"0"(-1:59)时,它将重新加载页面并在10分钟后重新开始.
It counts down every 10 minutes (again, according to the server) and displays the timer value. When it reaches "0" (-1:59) it will reload the page and start over at 10 minutes.
可能不是最干净的代码,但似乎工作得很好:
May not be the cleanest code, but it seems to work pretty well:
<script>
var currentMin = @DateTime.Now.Minute;
var currentSec = @DateTime.Now.Second;
var minutesLeft = 0;
var secondsLeft = 0;
$(document).ready(function() {
setupTimer();
});
function setupTimer() {
var baseMins = 0;
if (currentMin >= 0 && currentMin <= 9)
baseMins = 10;
else if (currentMin >= 10 && currentMin <= 19)
baseMins = 20;
else if (currentMin >= 20 && currentMin <= 29)
baseMins = 30;
else if (currentMin >= 30 && currentMin <= 39)
baseMins = 40;
else if (currentMin >= 40 && currentMin <= 49)
baseMins = 50;
else if (currentMin >= 50 && currentMin <= 59)
baseMins = 60;
minutesLeft = (baseMins - 1) - currentMin;
secondsLeft = 60 - currentSec;
setInterval(decreaseTimer, 1000);
}
function decreaseTimer() {
secondsLeft--;
if (secondsLeft == -1) {
minutesLeft--;
secondsLeft = 59;
}
if (minutesLeft == -1 && secondsLeft == 59) {
location.reload();
}
displayTimer();
}
function displayTimer() {
if (minutesLeft == 0)
minutesLeft = "0";
if (secondsLeft < 10)
secondsLeft = "0" + secondsLeft;
var timerDisplay = minutesLeft + ":" + secondsLeft;
$("#timer").text(timerDisplay);
}
</script>
这篇关于jQuery计时器/倒计时(使用服务器时间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!