在Meteor中显示客户端上的服务器时间 [英] Display server time on client in Meteor

查看:65
本文介绍了在Meteor中显示客户端上的服务器时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Meteor,在显示服务器时间的客户端上保持运行时钟(h:m:s)的有效方法是什么?

Using Meteor, what would be an efficient way to keep a running clock (h:m:s) on the client that displays the server's time?

我发现的JavaScript / PHP答案通常涉及定期获取服务器时间并计算它与客户端之间的差异。

The JavaScript/PHP answers I've found typically involve getting the server time periodically and calculating the difference between that and the client.

Meteor会是什么样子?

What would that look like with Meteor?

更新:自从我最初以来发生了很多变化发布了这个问题。如果您对预构建的解决方案感兴趣,我建议您查看 Meteor Timesync @mizzao。通过在控制台中运行 meteor add mizzao:timesync 来安装它。

UPDATE: A lot has changed since I originally posted this question. If you're interested in a pre-built solution, I recommend taking a look at Meteor Timesync by @mizzao. Install it by running meteor add mizzao:timesync in your console.

推荐答案

David Greenspan在这个关于Spark的演示文稿中获得客户时间14:30。我稍微修改了这段代码以获得服务器端时间:

David Greenspan gets the client time in this presentation on Spark around 14:30. I've modified this code slightly to get server side time:

Javascript:

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>

这篇关于在Meteor中显示客户端上的服务器时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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