2017-05-31 67 views
-2

當溫度增加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

是的setInterval沒有設置間隔 –

+0

變化'setInterval',而不是'設置interval' .JavaScript區分大小寫。並且首先更改'if',如果小寫'if' – prasanth

+0

ya感謝您的建議 – user3488168

回答

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); 
+0

感謝您的邏輯,但是在這裏我需要首先檢查temp,然後進入循環。因爲在臨時增加之後,我只能拿走最後30秒的數據。 – user3488168

相關問題