Meteor:如何在onRendered()中创建clearInterval()onDestroyed() [英] Meteor : How to clearInterval() onDestroyed() created in onRendered()

查看:98
本文介绍了Meteor:如何在onRendered()中创建clearInterval()onDestroyed()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每秒都有一个倒计时功能,所以我提供了 setInterval()。移动到另一个模板后,间隔功能继续运行。如何销毁它 onDestroyed()。下面的代码将帮助您理解。

I have a countdown function to run every sec, So i proffered setInterval(). After I moved to another template, the interval function keep on running. How to destroy it onDestroyed(). Below code will help you to understand well.

<template name="Home">
    <h4>{{timeremaining}}</h4>
</template>


Template.Home.helpers({
  timeremaining : function(){
    return Session.get('timeremaining');
  }
});

Template.Home.onRendered(function () {

      // time functions begin
      var end_date = new Date(1476337380000); // I am getting timestamp from the db.

      var run_every_sec = setInterval(function () {

        var current_date = new Date();
        var remaining = end_date.getTime() - current_date.getTime();

        var oneDay = 24*60*60*1000;
        var diffDays = Math.round(Math.abs(remaining/oneDay));

        console.log(remaining); // am getting this log in every template.

        if (remaining > 0) {
          //set remaining timeLeft
          Session.set('timeremaining',diffDays + ' Days ' + (Math.abs(end_date.getHours()-current_date.getHours())).toString() + ' Hrs ' + (Math.abs(end_date.getMinutes()-current_date.getMinutes())).toString() + ' Min ' + (60 - end_date.getSeconds()-current_date.getSeconds()).toString() + ' Sec ')
        } else {
          clearInterval(run_every_sec);
        }

      }, 1000);
      //time functions end



}.bind(this));

Template.Home.onDestroyed(function () {
  clearInterval(run_every_sec); // Here I cant remove this time interval
});

我们可以将 run_every_sec 声明为全局函数。如果是这样如何传递 end_date 。我认为在 run_every_sec 中声明 end_date 是不明智的,因为它来自db。

We can declare run_every_sec as global function. If so How to pass end_date. I dont think its wise idea to declare end_date inside the run_every_sec because its coming from db.

推荐答案

如果将间隔存储在Repo建议的文件范围内,如果一次有多个模板实例,则会出现问题:两个实例都将使用相同的 run_every_sec 变量。在这种情况下,您需要在模板实例上存储间隔,可以在里面访问 onRendered onDestroyed

If you store the interval in file scope like Repo suggested, you'll have problems if there's ever more than one instance of the template at a time: both instances will use the same run_every_sec variable. In this case, you'll need to store the interval on the template instance, which can be accessed as this inside onRendered and onDestroyed:

Template.Home.onRendered(function () {
    this.run_every_sec = setInterval(/* ... */);
});

Template.Home.onDestroyed(function () {
    clearInterval(this.run_every_sec);
});

这样,模板的每个实例都有自己的 run_every_sec property。

That way, each instance of the template will have its own run_every_sec property.

这篇关于Meteor:如何在onRendered()中创建clearInterval()onDestroyed()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆