0
我正在寻找一个拍卖网站的倒计时器,当用户更改他们的本地计算机时钟时不会更改。拍卖网站的PHP/jquery倒数计时器
我已经使用Keith Woods进行服务器同步,但是想知道是否有人对任何其他人有过任何经验,因为即使脚本在用户更改时钟时发生了变化。
我想像eBay一样。
干杯
我正在寻找一个拍卖网站的倒计时器,当用户更改他们的本地计算机时钟时不会更改。拍卖网站的PHP/jquery倒数计时器
我已经使用Keith Woods进行服务器同步,但是想知道是否有人对任何其他人有过任何经验,因为即使脚本在用户更改时钟时发生了变化。
我想像eBay一样。
干杯
取决于你想如何准确(毫秒等),这是一个简单的setInterval
在您的倒计时功能,可以一定程度上得到准确的结果(Doc),并不能由一个人改变他们的时钟被改变。
由于此方法依赖于javascript执行,因此可能会延迟页面加载速度或其他导致其不同步的情况。但是,如果你每隔一分钟左右对网络服务器进行一次AJAX调用,就可以重新同步客户端上的时间,以确保没有发生明显的延迟。
所以干脆:
var currentTime = [6,50,20]; //[Hours,Minutes,Seconds]
var intervalId;
function updateTime()
{
currentTime[2]--;
if (currentTime[0] == 0 && currentTime[1] == 0 && currentTime[2] == 0)
{
clearInterval(intervalId);
//do your processing of countdown end here
}
if (currentTime[2] == -1)
{
currentTime[2] = 59;
currentTime[1]--;
}
if (currentTime[1] == -1)
{
currentTime[1] = 59;
if (currentTime[0] > 1)
{
currentTime[0]--;
}
}
//do your processing of current time here (ie. display on screen)
}
//Use this function as the callback to an AJAX request to the server
//This will sync the time correctly
function ajaxCurrentTime(AJAXResponse)
{
//For this example, we will say the AJAXResponse is a JSON string
currentTime = JSON.parse(AJAXResponse);
}
intervalId = setInterval(updateTime,1000);
我将是第一个承认这是不是因为在大多数但不接触的日期/时间的几秒钟潜在的不准确的完美解决方案客户机不会因为他们改变时间而受到影响。
我提供的代码不包括实际进行AJAX通信的部分,只是它的回调。这只是一个可以实现的方式的例子,没有真正的错误检查,并且可能有其他我没有注意到的问题,可以用它作为指导。