使用 Meteor,在显示服务器时间的客户端上保持运行时钟 (h:m:s) 的有效方法是什么?
我发现的 JavaScript/PHP 答案通常涉及定期获取服务器时间并计算该时间与客户端之间的差异。
Meteor 会是什么样子?
更新:自从我最初发布这个问题以来,发生了很多变化。如果您对预先构建的解决方案感兴趣,我建议您查看 Meteor Timesync通过@mizzao。通过在控制台中运行 meteor add mizzao:timesync
来安装它。
最佳答案
David Greenspan 在 presentation on Spark 中获得 客户 时间14:30左右。我稍微修改了这段代码以获得服务器端时间:
Javascript:
if (Meteor.isClient) {
Meteor.startup(function () {
setInterval(function () {
Meteor.call("getServerTime", function (error, result) {
Session.set("time", result);
});
}, 1000);
});
Template.main.time = function () {
return Session.get("time");
};
}
if (Meteor.isServer) {
Meteor.methods({
getServerTime: function () {
var _time = (new Date).toTimeString();
console.log(_time);
return _time;
}
});
}
还有 HTML:
<body>
{{> main}}
</body>
<template name="main">
{{time}}
</template>
关于javascript - 在 Meteor 的客户端上显示服务器时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14226176/