如何使用流星定期更新变量

编程入门 行业动态 更新时间:2024-10-18 14:26:31
本文介绍了如何使用流星定期更新变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

如何使用流星定期更新变量

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

发布评论

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

>www.elefans.com

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