倒数计时到期后发送电子邮件 [英] Sending an Email after countdown expires

查看:111
本文介绍了倒数计时到期后发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的CountDown遇到问题。计数器过期(t< = 0)时,我曾尝试向以前插入收藏夹的人发送电子邮件。问题是,只有当我访问该页面时,它才会被触发。即使没有人在客户端,服务器是否也可能发送电子邮件?我希望我必须在服务器端构建一些东西?

I encountered a problem with my CountDown. I tried to send emails to some people i inserted in my collection before, when the counter expires (t<=0). The problem is, its only getting fired when iam visiting the page. Is it anyhow possible that the server sends the emails when even noone is on the client side? I expect that i have to build something on the server side?

      Template.decision.onRendered(function(){
         clearInterval(timeinterval);
         timeinterval = setInterval(function () {
         var endtime = '2016/02/10'     
          Meteor.call("getCurrentTime", function (error, result) {
            Session.set("time", result);
            var t = getTimeRemaining(endtime);
            Session.set("t", t);
          });
        }, 1000);
      });


  function getTimeRemaining(endtime){
    var t = Date.parse(endtime) - Session.get('time');
    var seconds = ("0" + Math.floor( (t/1000) % 60 )).slice(-2);
    var minutes = ("0" + Math.floor( (t/1000/60) % 60 )).slice(-2);
    var hours = ("0" + Math.floor( (t/(1000*60*60)) % 24 )).slice(-2);
    var days = Math.floor( t/(1000*60*60*24) );
    console.log(t);

    if(t <= 0 && timeinterval) {
      clearInterval(timeinterval);
      var to = Questions.findOne({_id:selectedDecisionId}).email;
      var questionText = Questions.findOne({_id:selectedDecisionId}).questionDB;
      var nameCreater = Questions.findOne({_id:selectedDecisionId}).name;
      Meteor.call('sendEmail',to,questionText,nameCreater);
    }

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

在我的服务器上:

  Meteor.methods({
    'getCurrentTime': function (){
      return Date.parse(new Date());
    }
  });


推荐答案

更好的方法是使用<服务器上的em> cron作业。使用 percolate:synced-cron 包很容易做到这一点。

The better way to approach this is by using a cron job on the server. This is easily doable using the percolate:synced-cron package.

SyncedCron.add({
  name: 'Send emails',
  schedule: function(parser) {
    // parser is a later.parse object
    return parser.text('every 1 minute');
  },
  job: function() {
    // find relevant, expired documents
    // send an email to each user that is affected
  }
});

这篇关于倒数计时到期后发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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