有谁知道一个好的 JSON 时间服务器? [英] Does anyone know of a good JSON time server?

查看:19
本文介绍了有谁知道一个好的 JSON 时间服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 JSON 获取当前时间(从可靠来源).精确时间在我的应用程序中是关键任务,所以我不能依赖设备的时间,即使它只有一两秒钟.

I need to obtain current time (from a credible source) using JSON. Precise time is mission-critical in my application so I cannot rely on the time of the device, even if it is only a second or two off.

我并不担心精度",而只是担心运行该应用程序的多个设备具有相同的时间.

I am not as worried about 'precision' rather just so that several devices running the app have the same time.

推荐答案

function getTime(zone, success) {
    var url = 'http://json-time.appspot.com/time.json?tz=' + zone,
        ud = 'json' + (+new Date());
    window[ud]= function(o){
        success && success(new Date(o.datetime));
    };
    document.getElementsByTagName('head')[0].appendChild((function(){
        var s = document.createElement('script');
        s.type = 'text/javascript';
        s.src = url + '&callback=' + ud;
        return s;
    })());
}

getTime('GMT', function(time){
    // This is where you do whatever you want with the time:
    alert(time);
});

从这里

这篇关于有谁知道一个好的 JSON 时间服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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