Meteor模板自动运行(会话变量)
假设我有一个会话变量,它保存一个图像源。每秒钟,我都希望包含此会话的帮助程序能够运行Meteor模板自动运行(会话变量),meteor,Meteor,假设我有一个会话变量,它保存一个图像源。每秒钟,我都希望包含此会话的帮助程序能够运行 if (Meteor.isClient) { Template.TargetTask.helpers({ 'imageSrc': function (e, template) { var clock = setTimeout(function() { var position = IMAGE_POOL.pop(); Session.set("current
if (Meteor.isClient) {
Template.TargetTask.helpers({
'imageSrc': function (e, template) {
var clock = setTimeout(function() {
var position = IMAGE_POOL.pop();
Session.set("currentTarget", position);
}, 1000);
var position = Session.get("currentTarget");
var imageSrc = '/' + position + '.bmp';
return imageSrc;
}
});
图像源来自全局图像池。但是,池可能连续包含两个相同的映像。在这种情况下,将使用相同的参数调用Session.set()
,会话将保持不变
Q1。当会话变量保持不变时,即使调用了Session.set()
,模板帮助程序也不会自动运行吗?
Q2。如果是这样,我应该如何使它在每次弹出新图像时运行?否,跟踪器
计算不会在值不变时失效
Session.set('test', false);
Tracker.autorun(function() { console.log(Session.get('test')) }); //Logs 'false'
Session.set('test', false); //Nothing
Session.set('test', true); //Logs true
在您的例子中,如果您想保留此代码结构(对我来说似乎有点重),您可以使用时间戳存储一个对象:
if (Meteor.isClient) {
Template.TargetTask.helpers({
'imageSrc': function (e, template) {
var clock = setTimeout(function() {
var position = IMAGE_POOL.pop();
Session.set("currentTarget", {
position : position,
timestamp : Date.now()
});
}, 1000);
var position = Session.get("currentTarget").position;
var imageSrc = '/' + position + '.bmp';
return imageSrc;
}
});
}