JavaScript NTP时间 [英] JavaScript NTP time

查看:1048
本文介绍了JavaScript NTP时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个计算脚本来计算旧日期和今天之间的时间。

一切正常,直到我在错误日期的计算机上测试并看到结果。

所以我找到了一种通过 http://json-time.appspot获取NTP时间的方法.com / time.json

问题是我需要每毫秒的当前时间,因为我想要计算毫秒但是每隔几毫秒就不可能向NTP服务器发送请求。

这是一些示例代码,用于查看我正在撰写的内容

  var今天; 
$(document).ready(function(){

$ .data = function(success){
$ .get(http://json-time.appspot .com / time.json?callback =?,function(response){
success(new Date(response.datetime));
},json);
};
});

函数更新(){
var start = new Date(March 25,2011 17:00:00);
// var today = new Date();
$ .data(函数(时间){
今天=时间;
});
var bla = today.getTime() - start.getTime();
$(#milliseconds)。text(bla);
}

setInterval(update(),1);


解决方案

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



<第二,即使你可以,这也不是一个好主意:你将从使用这个脚本的每台计算机每秒发出1000个请求。即使客户端及其连接可以处理这个问题,它也只是JSON服务器的DDoS。



你可以做的是:




  • 从JSON-NTP获得时间(一次),这将是一个日期

  • 获取当地时间(一次),这个将是一个日期

  • 计算NTP与本地时间之间的差异(一次),这可能是本地时间关闭的毫秒数

  • 对于每次计算,考虑差异


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);

解决方案

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

这篇关于JavaScript NTP时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆