我有一个 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 值,然后再次发送电子邮件。
我怎样才能做到这一点?
答案 0 :(得分:0)
您可以设置一个标志变量来记录是否已经发送了有关温度违规的电子邮件。
一旦温度降至 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;
}
}