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

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

问题描述

我有一个会话变量,我想以固定的周期进行更新.说,我希望此变量每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.

在我看来,执行此操作的最佳位置是在相关模板的帮助器部分内.我首先尝试使用setInterval来执行此操作,如此处所述,但这是行不通的(该功能只是没有似乎重复).

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.

(如果有一个直接而完全不同的解决方案在流星上工作,我不知道,所以如果我缺少明显的东西,请向我指出.)

(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);
};

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

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