Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
48
rated 0 times [  54] [ 6]  / answers: 1 / hits: 19499  / 13 Years ago, sat, april 2, 2011, 12:00:00

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

More From » jquery

 Answers
7

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


[#92945] Friday, April 1, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yvettel

Total Points: 517
Total Questions: 101
Total Answers: 102

Location: Vanuatu
Member since Wed, Oct 14, 2020
4 Years ago
;