[英]How to do setTime intervel for mail sending -js
I'm developing leave system for staff.If staff request leave to the HR , when HR is not approval their leave request ,from that time every one hours mail send to the HR for remainder for approval or reject that leave request. 我正在为员工开发请假系统。如果员工要求向人力资源部请假,则在人力资源部未批准其请假请求时,从那时起,每隔一小时将邮件发送给人力资源部以供其他人批准或拒绝该请假请求。
Here is my problem i tried to send mail every one minute interval .its working.but it is continually sending mail.that's mean not working my condition. 这是我尝试每隔一分钟发送一次邮件的问题。它正常工作。但是它一直在发送邮件。这意味着我的状况不正常。
My conditions are .. 我的条件是..
if pending my leave request till (leave status=1 ),then send every one hours . 如果等待我的休假请求直到(leave status = 1),则每隔一小时发送一次。
if approved my request (leave status=3 )then stop send mail. 如果批准了我的请求(状态为3),则停止发送邮件。
Here is my code 这是我的代码
//mail sending function ............ //邮件发送功能............
function(err, res) {
setInterval(function(){
if((ctx.result.leave_status === 1)&&!(ctx.result.leave_status === 3) )
{
console.log('resp: ' + emailsArray.toString());
type = res.type.description;
name = res.name.f_name;
LeaveDetails.app.models.Email.send({
to: emailsArray.toString(),
from: 'gnanaseelan@gmail.com',
subject: 'Leave Request ',
// text: 'my text',
html: '<h1 style="text-align:center;"> Report leave request</h1><h3>' + name + ' have requested leave</h3><ul><li><b>type: </b>' + type + '</li><li><b>duration: </b>' + duration + '</li><li><b>Number of days: </b>' + days + '</li></ul>,
},
function(err, mail) {
console.log('email sent!');
if(err !== null) {
log.error({
type: 'error',
model: 'leave-details',
method: 'create (email send)',user_id: ctx.result.user_id,
err: err,leaveData: ctx.result,}, 'leave request-email-failed');
}
else {
log.info({
type: 'success',
model: 'leave-details',
method: 'create (email send)',
user_id: ctx.result.user_id,
leaveDuration: ctx.result.leave_start_time + " " + ctx.result.leave_end_time,
err: err
}, 'leave request-email-success');
}
next(err);
});
}//if end
},60000);
}
您必须清除clearInterval才能停止发送电子邮件,最好的技巧是将时间间隔设置为构造函数中的全局对象,然后在设置某些状态时清除它,例如,如果完成设置为“ true”,则状态为“ finished:false” “,componentWillUpdate中的clearInterval或shouldComponentUpdate中的,如果不这样做,您的间隔永远不会结束,直到您卸载时才会发生
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.