当温度上升1度时,我只需要从机器中取出最后30秒的数据并将其发送到我的数据库。我错过了什么吗?
set interval(function x(){
If(current_temp != prev_temp){
if((current_temp-prev_temp)>1 || (current_temp - prev_temp)<1){
Console.log('send data to end point egress ');
}
console.log('send id to the end point');
}
}),30000)
答案 0 :(得分:0)
更正了拼写错误,并初始化了变量。
var current_temp = 20;
var prev_temp = 18;
setInterval(function(){
if(current_temp !== prev_temp){
if((current_temp - prev_temp) > 1 || (current_temp - prev_temp) < 1){
console.log('send data to end point egress');
}
console.log('send id to the end point');
}
}, 3000);
虽然如果你想检查温度是否改变1度,你应该使用这样的东西:
var prev_temp = 50;
var current_temp = null;
setInterval(function(){
current_temp = Math.floor((Math.random() * 100) + 1); // generate random number between 1 - 100
if((current_temp - prev_temp) === 1){ // send data only when the difference is 1 ??
console.log('send data to end point egress');
}
console.log('send id to the end point');
prev_temp = current_temp; // update previous temperature with the current one
}, 3000);