JavaScript NTP 时间

发布于 2024-10-28 12:04:16 字数 977 浏览 2 评论 0原文

我正在编写一个计数脚本,用于计算旧日期和今天之间的时间。
一切都运行良好,直到我在日期错误的计算机上进行测试并看到结果。
所以我找到了一种通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱格式化 2024-11-04 12:04:17

我不确定您是否理解 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.

花伊自在美 2024-11-04 12:04:16

首先,JS 调度程序具有一定的粒度 - 也就是说,您可以请求小于 20 毫秒的间隔,但它不会立即触发 - 您可以看到每 20 毫秒触发 20 个事件。

其次,即使可以,这也不是一个好主意:您将每秒从使用此脚本的每台计算机发出 1000 个请求。即使客户端及其连接可以处理此问题,这对于 JSON 服务器来说也无异于 DDoS。

你可以做的是:

  • 从 JSON-NTP 获取时间(一次),这将是一个日期
  • 获取本地时间(一次),这将是一个日期
  • 计算 NTP 和本地时间之间的差异(一次),这可能是 每次计算时当地时间偏离的毫秒数
  • ,考虑差异

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:

  • 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
滥情稳全场 2024-11-04 12:04:16

将其放在文档的顶部:

var clientTime = new Date();

并将其放在文档的底部:

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)

然后,如果您需要知道某件事的持续时间:

var startTime = new Date();

// [processing...]

var endTime = new Date();
var duration = endTime - startTime; // in milliseconds
var startTimeServer = startTime + deltaTime;
var endTimeServer = endTime + deltaTime;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文