如何使用meteor定期更新变量

编程入门 行业动态 更新时间:2024-10-18 12:31:17
本文介绍了如何使用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定期更新变量

本文发布于:2023-11-23 20:00:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622710.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   变量   meteor

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!