【问题标题】:Checking and sending email on interval if certain conditions are met如果满足某些条件,则定期检查和发送电子邮件
【发布时间】:2021-08-25 01:29:43
【问题描述】:

我有一个 express js 程序,其中一个函数以 10 秒的间隔运行并获取温度值。

已经默认定义了温度阈值、上限阈值和下限阈值。

temperature_threshold = 30
upper_threshold = temperature_threshold + 2
lower_threshold = temperature_threshold - 2

现在在每个时间间隔,如果当前温度值超过温度阈值,则发送温度超过阈值的电子邮件。

if current_temperature >= temperature_threshold
{
    send email;
}

发送一次邮件后,如果温度值在lower_threshold和upper_threshold之间,则不要发送邮件。

但如果温度下降到 lower_threshold 水平以下,然后再次升高到超过 temperature_threshold 值,然后再次发送电子邮件。

我怎样才能做到这一点?

【问题讨论】:

    标签: javascript node.js algorithm express


    【解决方案1】:

    您可以设置一个标志变量来记录是否已经发送过温度违规的电子邮件。

    一旦温度低于lower_threshold,重置标志。

    const temperature_threshold = 30;
    const upper_threshold = temperature_threshold + 2;
    const lower_threshold = temperature_threshold - 2;
    
    // whether an email has already been sent
    let sent = false;
    
    // the function runs every 10 seconds
    function check(current_temperature) {
        if (current_temperature <= lower_threshold) {
            sent = false;
        } else if (current_temperature >= temperature_threshold && !sent) {
            /* send email */
            sent = true;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-18
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多