Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor模板自动运行(会话变量)_Meteor - Fatal编程技术网

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