JavaScript NTP 时间
我正在编写一个计数脚本,用于计算旧日期和今天之间的时间。
一切都运行良好,直到我在日期错误的计算机上进行测试并看到结果。
所以我找到了一种通过 http://json-time.appspot.com/time 获取 NTP 时间的方法.json。
问题是我每毫秒都需要当前时间,因为我想计算毫秒数,但不可能每毫秒向 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);
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
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定您是否理解 NTP 的用途:即计算机内部时钟的同步,而不是时钟本身的用途。
我建议您连接到 NTP 服务一次,以获取客户端内部时间的差异,并使用它来更正显示。但我不太确定为什么与客户端计算机时间的比较还不够。
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.
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.
首先,JS 调度程序具有一定的粒度 - 也就是说,您可以请求小于 20 毫秒的间隔,但它不会立即触发 - 您可以看到每 20 毫秒触发 20 个事件。
其次,即使可以,这也不是一个好主意:您将每秒从使用此脚本的每台计算机发出 1000 个请求。即使客户端及其连接可以处理此问题,这对于 JSON 服务器来说也无异于 DDoS。
你可以做的是:
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.
Second, even if you could, this is not a good idea: you would be making 1000 requests every second, from every computer which uses this script. Even if the client and their connections could handle this, it's nothing short of a DDoS for the JSON server.
What you could do is this:
将其放在文档的顶部:
并将其放在文档的底部:
然后,如果您需要知道某件事的持续时间:
put this right at the top of your document:
and this right at the bottom of your document:
then, if you need to know the duration of something: