使用ajax进行状态检查

时间:2018-02-07 12:56:03

标签: javascript jquery

我有一个rpi,它以每5秒的当前时间更新我的数据库,它保存在变量cseen中,并且如果主板成功更新数据库,则每5分钟更改一次。这一点是,如果5秒后值相同,则电路板必须断开连接。

检查变量是否已更改的代码是此处的问题,如下所示:



<script>
var lastSeen = "";
var cseen = "";
 setInterval(function(){
    $.getJSON('temp.php',function(data){
        tot = data.length;
        for(var i = 0; tot > i; i++){
            //console.log(data[i][0]);
            cseen = data[i][1];
            
            $("#temp" + i).html(data[i][0]);
            if(cseen != lastSeen){
                console.log("board " + i + "cseen:" + cseen +" lastSeen:"+ lastSeen);
                console.log("board " + i + " Online");
            }else{
                console.log("board " + i + "cseen:" + cseen +" lastSeen:"+ lastSeen);
                console.log("board " + i + " Offline");
            }
            lastSeen = cseen;
        }
        //console.log(data.length);
        //$(".temp").html(data.value);
    })
 },5000)
</script>
&#13;
&#13;
&#13;

我从控制台收到的输出是

&#13;
&#13;
(index):50 board 0cseen:1 lastSeen:1
(index):51 board 0 Offline
(index):50 board 1cseen:1 lastSeen:1
(index):51 board 1 Offline
(index):50 board 2cseen:1 lastSeen:1
(index):51 board 2 Offline
(index):50 board 3cseen:1 lastSeen:1
(index):51 board 3 Offline
(index):47 board 4cseen:Wed Feb  7 12:54:21 2018 lastSeen:1
(index):48 board 4 Online
(index):47 board 5cseen:1 lastSeen:Wed Feb  7 12:54:21 2018
(index):48 board 5 Online
(index):50 board 6cseen:1 lastSeen:1
(index):51 board 6 Offline
&#13;
&#13;
&#13;

编辑:这是记录到控制台的json数据:

&#13;
&#13;
(7) [Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2)]
(2) ["400'C", "1"]
(2) ["20", "1"]
(2) ["30", "1"]
(2) ["60", "1"]
(2) ["46.2'C↵", "Wed Feb  7 13:00:44 2018"]
(2) ["70", "1"]
(2) [null, "1"]
&#13;
&#13;
&#13;

实际在线和更新的唯一主板是主板5,请告诉我我做错了什么,这是一个纯粹的逻辑问题

1 个答案:

答案 0 :(得分:0)

这应该对你有所帮助。显然我不得不创造一个&#34;假的&#34;例如,因为我无法通过ajax调用您的JSON数据,但希望这能够充分展示您需要做的事情,以使其适应您的需求。如果没有,我可以写出我认为应该是&#34;真实&#34;代码也是如此,但显然无法测试它。

您目前正在做的是将每个电路板相互比较,而不是比较它们之前的状态。要启用它,您需要将每个板的先前状态存储在一个数组中,该数组保留在ajax请求之间,并在每个请求结束时使用最新数据覆盖。然后你在&#34;当前&#34;之间进行比较。和&#34;最后&#34;每个单独的董事会的状态。

您可以运行此代码段并观察输出:

&#13;
&#13;
//imagine this was the data written last time getJSON was called. 
//When you make your real code, you should declare it as null to begin with, as you will have no previous data at startup.
var lastState = [
  ["300'C", "1"],
  ["40", "1"],
  ["50", "1"],
  ["30", "1"],
  ["44.2'C", "Wed Feb 7 12:55:44 2018"],
  ["53", "1"],
  [null, "1"]
];

//now imagine this is within the "getJSON" callback, as if it has just come from the server
var data = [
  ["400'C", "1"],
  ["20", "1"],
  ["30", "1"],
  ["60", "1"],
  ["46.2'C", "Wed Feb  7 13:00:44 2018"],
  ["70", "1"],
  [null, "1"]
];

//if there's no previous data, we won't do anything except create it for next time. If there is some previous data, do the comparisons
if (lastState) {

  for (var i = 0; data.length > i; i++) {
    var curState = data[i][1];
    var brdText = "board " + i;
    var statusText = " Offline";

    $("#temp" + i).html(data[i][0]);
    console.log(brdText + " current State:" + curState + " last State:" + lastState[i][1]);

    //compare to the lastState entry for the same index
    if (curState != lastState[i][1]) {
      statusText = " Online";
    }
    console.log(brdText + statusText);
  }
}

lastState = data; //save the state as the "last state" for the next request

//this would be the end of your getJSON callback.
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;