简体   繁体   English

流星应用程序中单个页面上的多个计时器

[英]Multiple Timers On A Single Page In A Meteor Application

I am trying to display multiple timers on a template. 我正在尝试在模板上显示多个计时器。 My template onCreated function is given below 我的模板onCreated函数如下

   Template.viewTeamTermPaper.onCreated(function(){
     let template = Template.instance();
     template.subscribe('getTeamTermPaper');
     template.lastDateSubmit = new ReactiveVar()
     template.timeInterval = new ReactiveVar();
     template.timeLapse = new ReactiveVar()
     template.autorun(function () {
        if (template.lastDateSubmit.get()){
           template.timeInterval.set(setInterval(function() {
           let todayDate = new Date()
           //Session.set("time" , todayDate);
           template.timeLapse.set(getTimeRemaining(template.lastDateSubmit.get()));
        //Session.set("timeLapse" , timeLapse);
   } , 1000));
 }

}); });

}); });

I am displaying the each instance of the template with an helper called lastDate(). 我正在使用名为lastDate()的助手来显示模板的每个实例。

   Template.viewTeamTermPaper.helpers({
     lastDate(){
     let papers =  TeamTermPaper.find({}).fetch();
     return papers.map(function(paper){
     let obj = {}
     obj.paper = paper;
     Template.instance().lastDateSubmit.set(paper.last_submission_date);
     obj.time = Template.instance().timeLapse.get()
  //arr.push(obj)
     return obj
});

//return arr;

} }

}); });

I have a function which calculates the elapsed time. 我有一个计算经过时间的函数。

     function getTimeRemaining (endtime){
        let t = Date.parse(endtime) - new Date();
        let seconds = ("0" + Math.floor((t/1000) % 60)).slice(-2);
        let minutes = ("0" + Math.floor((t/1000/60) % 60)).slice(-2);
        let hours =   ("0" + Math.floor((t/(1000 * 60 * 60)) %   24)).slice(-2);
        let days = Math.floor(t/(1000*60*60*24));

        if (t <= 0){
            //clearInterval(timeInterval);
        }

        return {
            'total' : t,
            'days'  : days,
            'hours' : hours,
            'minutes': minutes,
            'seconds' : seconds
        }

} }

This is how i out put the instance of the template 这就是我放出模板实例的方式

  <template name="viewTeamTermPaper">
    {{#each lastDate}}
        {{> studentViewTeamPaper}}
    {{/each}}
  </template>

My problem is i have the same timer for each item that is contained in the lastDate helper instead of different count down timers. 我的问题是我为lastDate帮助器中包含的每个项目使用相同的计时器,而不是使用不同的倒数计时器。 I was thinking maybe for each item in my collection i should create a reactive variable that can be stored in an array. 我在想,也许对于我收藏中的每个项目,我都应该创建一个可存储在数组中的反应性变量。 I don't know how to go about this line of thought. 我不知道该怎么走。 Any help is really appreciated. 任何帮助都非常感谢。

 <template name="studentViewTeamPaper">
   <div class="row">
      <div class="col-md-12 card">
          <div class="table-responsive">
            <table class="table table-bordered">

               <thead>
                 <tr>
                  <th class="text-danger">Term Paper Name</th>
                  <th class="text-danger">View</th>
                  <th class="text-danger">Count Down</th>
                </tr>
               </thead>


             <tbody>

               <tr>
                  <td>

                      {{paper_name}}


                  </td>

                  <td>
                      {{last_submission_date}}

                  </td>

                  <td></td>
              </tr>

            </tbody>
          </table>
        </div>


     </div>
   </div>
 </template>

You're trying to control everything from the parent template instead of from the individual item template. 您正在尝试通过父模板而不是单个项目模板来控制所有内容。 If you define the countdown timer at the lower level studentViewTeamPaper template then you'll automatically get a different timer for each. 如果您在较低级别的studentViewTeamPaper模板上定义倒数计时器,则每个计时器都会自动获得一个不同的计时器。

Also look at the remcoder:chronos package which makes time reactive. 还要看一下remcoder:chronos软件包,它使时间能够响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM