我正在写一个计数脚本,它计算旧日期和今天之间的时间.
一切顺利,直到我在错误的日期的计算机上测试并看到结果.
所以我找到了一种通过http://json-time.appspot.com/time.json获取NTP时间的方法.
问题是我需要每毫秒的当前时间,因为我想要计算毫秒但是每隔几毫秒就不可能向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);
Run Code Online (Sandbox Code Playgroud)