JavaScript NTP 时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5522191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript NTP time
提问by David
I'm writing a counting script who counts the time between an old date and today.
Everything worked good until I tested on a computer with wrong date and saw the results.
So I found a way to get NTP time via http://json-time.appspot.com/time.json.
The problem is that I need the current time every millisecond because I want to count the milliseconds but Its impossible the send request to the NTP server every milisecond.
This is some example code to see what I'm writing about
我正在编写一个计数脚本,用于计算旧日期和今天之间的时间。
一切都很好,直到我在错误日期的计算机上进行测试并看到结果。
所以我找到了一种通过http://json-time.appspot.com/time.json获取 NTP 时间的方法。
问题是我每毫秒都需要当前时间,因为我想计算毫秒数,但不可能每毫秒向 NTP 服务器发送请求。
这是一些示例代码,看看我在写什么
var today;
$(document).ready(function(){
$.data = function(success){
$.get("http://json-time.appspot.com/time.json?callback=?", function(response){
success(new Date(response.datetime));
}, "json");
};
});
function update(){
var start = new Date("March 25, 2011 17:00:00");
//var today = new Date();
$.data(function(time){
today = time;
});
var bla = today.getTime() - start.getTime();
$("#milliseconds").text(bla);
}
setInterval("update()", 1);
采纳答案by Piskvor left the building
First of all, the JS scheduler has a certain granularity - that is, you can request an interval smaller than, say, 20 msec, but it will not fire immediately - what you could see is 20 events fired off every 20 msec.
首先,JS 调度程序有一定的粒度——也就是说,你可以请求一个小于 20 毫秒的间隔,但它不会立即触发——你可以看到每 20 毫秒触发 20 个事件。
Second, even if you could, this is not a good idea: you would be making 1000 requests every second, from every computerwhich uses this script. Even if the client and their connections could handle this, it's nothing short of a DDoS for the JSON server.
其次,即使可以,这也不是一个好主意:您将每秒从使用此脚本的每台计算机发出 1000 个请求。即使客户端和他们的连接可以处理这个问题,对于 JSON 服务器来说也无异于 DDoS。
What you could do is this:
你可以做的是:
- get time from JSON-NTP (once), this will be a Date
- get local time (once), this will be a Date
- calculate the difference between NTP and local time (once), this will likely be the number of msec that local time is off
- for every time calculation, take the difference into account
- 从 JSON-NTP 获取时间(一次),这将是一个日期
- 获取当地时间(一次),这将是一个日期
- 计算NTP和本地时间(一次)之间的差异,这可能是本地时间关闭的毫秒数
- 对于每次计算,请考虑差异
回答by CAFxX
put this right at the top of your document:
把它放在文档的顶部:
var clientTime = new Date();
and this right at the bottom of your document:
就在您的文档底部:
var serverTime = new Date("<have the server put here its current date/time along its timezone>");
var deltaTime = serverTime - clientTime; // in milliseconds (expected accuracy: < 1 second)
then, if you need to know the duration of something:
然后,如果您需要知道某事的持续时间:
var startTime = new Date();
// [processing...]
var endTime = new Date();
var duration = endTime - startTime; // in milliseconds
var startTimeServer = startTime + deltaTime;
var endTimeServer = endTime + deltaTime;
回答by nfechner
I'm not sure you understand what NTP is for: Namely sychronization of the internal clock in the computer, not as use for a clock in itself.
我不确定您是否理解 NTP 的用途:即计算机内部时钟的同步,而不是用于时钟本身。
I would suggest, that you connect to the NTP service once to get the difference to the internal time of the client and use that to correct it for display. But I'm not exactly sure, why a comparison to the client computer time is not sufficient.
我建议您连接到 NTP 服务一次以获取与客户端内部时间的差异,并使用它来更正显示。但我不太确定,为什么与客户端计算机时间进行比较是不够的。