2016-05-03 39 views
0

我想仅显示问卷模板一段时间。从5月5日到5月9日。流星 - 在特定时间段内解锁模板(按日期)

我想像一个模板助手,如果5月5日开始,返回true,如果9月5日已过,则返回false。但是,我怎样才能有效地更新if语句的瞬时时间值呢?

Template.registerHelper("questionaireUnlocked", function() { 
    let now = new Date(); 
    let startDate; 
    let endDate; 
    if (now > startDate && now < endDate) { 
    return true; 
    } 
}); 

感谢您的帮助!

莫夫

回答

1

您可以使用自动运行时间分配给一个变量的反应,如会话父模板中的变量或ReactiveVar。

Template.myParentTemplateName.created = function() { 
    const template = this; 
    template.autorun(function() { 
    Meteor.setInterval(Session.set("newTime",new Date()), 6000);   
    }); 
} 

Template.myParentTemplateName.helpers ({ 
    "questionaireUnlocked" : function() { 
     const now = Session.get("newTime"); 
     ..... 
     if (now > startDate && now < endDate) { 
      return true; 
     } 
    } 
}) 

希望有所帮助。

+0

谢谢 - 但是当使用setInterval方法时,我在控制台中得到以下错误,并且时间没有更新:'setInterval回调中的异常:TypeError:func不是函数' – Raggamuffin

+0

明白了:setInterval需要一个函数作为第一个值,在一个匿名函数中使用Session.set工作:'Meteor.setInterval(()=> {Session.set(TIME_NOW,new Date())},6000);' – Raggamuffin

相关问题