如何使用定时器功能在一段时间内获取数据?

时间:2017-05-31 06:13:42

标签: javascript typescript timer

当温度上升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)

1 个答案:

答案 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);