我有一个setInterval
函数,它显示我网站上事件的剩余时间。但倒计时与第二秒的实际滴答不同。Javascript:知道实际秒数是否打勾?
我的代码使用ajax调用服务器来获得失效日期一次,并在其成功倒计时将开始。很好,直到那里。
var request = new XMLHttpRequest();
request.open('GET', 'https://my-website/service.php', true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
date = request.responseText;
timer = setInterval(showRemaining, 1000);//start the countdown
} else {
// We reached our target server, but it returned an error
}
};
但当setInterval
被调用的时候需要在同步与第二的实际全球打勾。
(我希望我是有意义的。我的意思是调用需要保持同步,每次在第二遍你的电脑或手机上的时钟!)
我怎样才能做到这一点?提前致谢!
你需要做一个初步'setTimeout'当前MS和MS旁边(即'1000-(新的Date()之间的差异getMilliseconds() )'),然后启动'setInterval'请注意,setTimeout有一个最小值,所以如果它小于该值到下一秒,请添加1000. –
https://stackoverflow.com/a/9647221/2181514 –
如果PC /手机的时钟是不同的,就像未来的一个星期?这里有一些建议如何找到你的服务器时钟和本地时钟之间的偏移量https://stackoverflow.com/questions/1638337/the-best-way-to-synchronize-client-side-javascript-clock-with-server -date –