更正了您的拼寫錯誤,並初始化了變量。
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);
是的setInterval沒有設置間隔 –
變化'setInterval',而不是'設置interval' .JavaScript區分大小寫。並且首先更改'if',如果小寫'if' – prasanth
ya感謝您的建議 – user3488168