[英]setTimeout only running with a console.log in Meteor
我在Meteor应用上有以下代码
Template.timer.helpers({
currentTime: function(){
//keep the current time stored in the session
timeout = Meteor.setTimeout(function () {
Session.set("now", new Date());
}, 1000);
console.log(Session.get('now'));
});
Template.timer.events({
'click': function(){
//some code
Session.set("now", new Date());
//some more code
}
});
当发生点击事件时,即使我只更新一次Session,我也会同时运行多个超时。
但是,如果我删除了timer.helpers中的console.log,则setTimeout根本不会更新会话(只有Click会更新)。
我想念什么? 有没有更好的方法来保存当前(反应性)客户端时间?
如果要每秒更新一次具有最新客户端时间的客户端会话,则可以改用Meteor.setInterval并在渲染模板后进行设置。 例如:
Template.timer.rendered = function() {
Meteor.setInterval( function() {
Session.set("now", new Date());
}, 1000);
};
Template.timer.helpers({
currentTime: function() {
return Session.get("now");
}
});
这基本上是反应性的本质。
当以反应方式重新运行的函数(包括帮助函数和自动运行块)在其代码中的任何位置获取反应对象的值时,该功能将注册为该对象的依赖项。 当对象的值更改时,该函数的计算将因值的更改而无效,并且该函数将自行重新运行。
因此,最重要的是,这里发生的情况是console.log
get
了会话变量的值,这使得每次set
helper都会重新运行。 但是,它还会设置一个超时回调,该回调本身set
变量的值set
为(确保与当前时间不同的值),这意味着辅助函数将在超时回调具有后立即再次调用自身。跑。
如果删除console.log
,那么还将删除Session.get
,它将删除依赖关系,并且在更改Session变量的值时(无论是通过超时回调还是其他方式),帮助程序将不再重新运行。
网上有很多资源可以帮助您了解反应性,这可能是学习流星的最基本概念。
https://meteorhacks.com/journey-into-meteors-reactivity.html http://manual.meteor.com/#deps-transparentreactiveprogramming http://richsilv.github.io/meteor/meteor-reactive-data-types /
Tracker.autorun(function(){
//keep the current time stored in the session
timeout = Meteor.setInterval(function () {
var se = Session.get("now");
if(se === true){
se = new Date();
console.log(se);
}else{
console.log("until click i start")
}
}, 1000);
//console.log(Session.get('now'));
});
Template.timer.events({
'click': function(){
console.log("clicked")
Session.set("now",true);
}
});
试试这个代码,这就是我的改变。
首先,我们只在事件内部设置一个true/false
会话,以告知用户何时单击按钮。
稍后,我们将会话放入Tracker.autorun(function(){})
,使用自动运行,我们会查找会话中的任何更改,并删除一段时间的超时,请点击此处
第三,在超时变量上,我们寻找变量的变化,如果"now" === true
,我们启动计时器,否则,我们发送一个友好的console.log
。
那对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.