10

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);
David
  • 103
  • 1
  • 1
  • 4
  • Just because you get time from a web server does *not* make it NTP. NTP is a very robust protocol. See http://en.wikipedia.org/wiki/Network_Time_Protocol – Matt Johnson-Pint Feb 12 '13 at 17:03

3 Answers3

12

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
Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
4

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;
CAFxX
  • 28,060
  • 6
  • 41
  • 66
  • 6
    this has nothing to do with NTP – timaschew Jun 01 '12 at 10:21
  • 2
    You dont understand the question – Crapo Wolf Oct 30 '19 at 12:51
  • @CrapoWolf I understand what the OP is trying to accomplish ("I'm writing a counting script who counts the time between an old date and today") and that the way the OP is trying to do it is not practical (sending a request every millisecond), and will probably not yield the desired outcome. So I point out how to do things differently to achieve what the OP is actually trying to do. If you have different needs and therefore my answer does not apply to *your* specific case feel free to create a new question. – CAFxX Nov 27 '19 at 05:16
  • 1
    @timaschew neither does the question, if you read it carefully. :D – CAFxX Nov 27 '19 at 05:18
2

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.

nfechner
  • 17,295
  • 7
  • 45
  • 64