gpt4 book ai didi

javascript - JavaScript 中的 Facebook 服务器时间

转载 作者:行者123 更新时间:2023-11-28 02:33:59 24 4
gpt4 key购买 nike

我试图在我的网站上显示与 Facebook 服务器同步的可变时钟。Facebook 服务器时间位于: https://api.facebook.com/method/fql.query?query=SELECT+now%28%29+FROM+link_stat+WHERE+url+%3D+%271.2%27&format=json

如何在不刷新页面的情况下使其每秒变化?

最佳答案

假设有一些非书面函数,它应该看起来像这样:

var requestBegin = Date.now();
getServertimeFromFacebook(function callback(fbTime) {
var requestEnd = Date.now();
var latency = (requestEnd - requestBegin) / 2;
var curDevicetime = Date.now(); // = requestEnd, of course
var difference = fbTime - latency - curDeviceTime;

function clock() {
var cur = Date.now();
var curFbTime = cur + difference;
show(curFbTime); // print, log, whatever
};
setInterval(clock, …); // you could use a self-adjusting clock
// by using a setTimeout for each tick
});

你可以做

show = function(t) { console.log(new Date(t).toString()); };
getServertimeFromFacebook = function(cb) {
ajax("https://api.facebook.com/method/fql.query?query=SELECT+now%28%29+FROM+link_stat+WHERE+url+%3D+%271.2%27&format=json", function(responsetext) {
var obj = JSON.parse(responsetext);
var ts = obj[0].anon,
tms = ts * 1000;
cb(tms);
});
};

关于javascript - JavaScript 中的 Facebook 服务器时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13711384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com