从全局变量中读取和保存列表

时间:2016-10-20 08:44:18

标签: node.js node-red

不是最好的做法,但我想要的是存储包含type和datetime的对象数组。我使用它来发送邮件,我不想垃圾邮件,但限制所以类型只发送邮件每15分钟:

var sendMail = true;

var emailType = "test" // set for test

var log = []
var tempLog = global.get("emaillog")

// read the log from global variables
if ( typeof tempLog !== 'undefined' && tempLog )
{
  log = tempLog
}

// search the log
for (var i = 0, len = log.length; i < len; i++) 
{
    var logElement = log[i]
    var logElementEmailType = logElement.Type
    var logElementEmailDateTime = logElement.DateTime

    var dif = new Date() - logElementEmailDateTime;

    if (logElementEmailType == emailType && Math.abs(dif/1000) < (60*5))
    {
        sendMail = false
    }
}

// add to log
var newLogElement = 
{
    DateTime: new Date(),
    Type: mailType
}

log.push(newLogElement)

// save global variable
global.set("maillog",log)

但这不起作用,狠狠地骂我:D

1 个答案:

答案 0 :(得分:1)

我相信dif是以毫秒为单位。你将它除以1000并得到微秒。