如何使用meteor定期更新变量 [英] How to periodically update a variable using meteor

查看:25
本文介绍了如何使用meteor定期更新变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个会话变量,我想以固定的周期进行更新.比如说,我希望这个变量每 60 秒增加 1.

I have a session variable that I want to update with a fixed periodicity. Say, I want this variable to be increased by 1 every 60 seconds.

在我看来,执行此操作的最佳位置是相关模板的 helpers 部分.我首先尝试使用 setInterval 来执行此操作,如此处所述,但这不起作用(该函数没有t似乎重复).

It seems to me that the best place for doing this is inside the helpers section of the relevant template. I first tried to do this using setInterval as described here, but that didn't work (the function just didn't seem to repeat).

然后我尝试了我认为是一个简单的解决方案,但这也不起作用.见下文.辅助变量currentPosition"应该返回当天的当前分钟(加上偏移量).但是,它仅在第一次调用模板以及在事件"部分定义的函数中更改会话变量偏移量"时执行此操作,该函数响应单击特定 div(下一步"按钮).

I then tried what I thought would be a straightforward solution, but this also doesn't work. See below. The helper variable 'currentPosition' is supposed to return the current minute of the day (plus an offset). However, it only does this when the template is first called and when the session variable 'offset' is changed in a function that's defined in the 'events' section, which responds to a click on a particular div (a 'next' button).

    currentPosition: function () {
        var d = new Date();
        var h = d.getHours();
        var m = d.getMinutes();
        var w = h * 60 + m;

        Session.set("position", w + Session.get("offset"));

        return Session.get("position");
    },

我认为上面的助手会每分钟主动更新currentPosition"的值.然而它没有.

I would think that the above helper would actively update the value for 'currentPosition' every minute. Yet it does not.

所以,我的问题是,如何让会话变量每 60 秒更改一次,将其增加 1,而会话变量的新值反映在应用程序中,当变量为调整.

So, my question is, how can I have a session variable change every, say, 60 seconds, increasing it by, say, 1, while the new value of the session variable is reflected within the app, when the variable is adjusted.

(如果有一个直接且完全不同的解决方案适用于meteor,我不知道,所以如果我遗漏了一些明显的东西,请指出.)

(If there's a straightforward and completely different solution which works in meteor, I'm not aware of it, so do point it out to me if I'm missing something obvious.)

推荐答案

尽管您使用的是反应式变量,但它只会设置一次 - 在第一次调用帮助程序时.因此它不会再次运行.您需要帮助程序外部的函数来为您设置变量.记住一条重要规则 - 助手应该永远副作用.

Although you are using a reactive variable, it only gets set once - when the helper is first called. Therefore it doesn't run again. You need a function outside of the helper to set variable for you. Remember one important rule - helpers should never have side effects.

这是一个关于如何创建计时器的完整工作示例:

Here's a complete working example of how to create a timer:

<body>
  {{> timer}}
</body>

<template name="timer">
  <p>Total Seconds: {{seconds}}</p>
</template>

js

Template.timer.helpers({
  seconds: function() {
    return Template.instance().seconds.get();
  }
});

Template.timer.created = function() {
  var self = this;
  this.seconds = new ReactiveVar(0);

  this.handle = Meteor.setInterval((function() {
    self.seconds.set(self.seconds.get() + 1);
  }), 1000);
};

Template.timer.destroyed = function() {
  Meteor.clearInterval(this.handle);
};

这篇关于如何使用meteor定期更新变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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