Search code examples
javascripttimeupdating

Javascript code to check whether the time is updating


I was wondering if anybody can help me to write a javascript code that will check whether the time on my HMI is updating every seconds, if the time stops it should alert the user. I have a string variable called "device.string.TIME" that my HMI is reading from the PLC over modbus, the format the HMI is reading is in strings "12:00:00". This string is updating continuously (1 second) from the PLC, now when this time gets stale for 2 seconds i want to alert the user.


Solution

  • var oldTime = 0;
    function checkTime() {
       var newTime = device.string.TIME;
       if(oldTime!=0&&newTime==oldTime) {
          alert("something!");
          return;
       }
       oldTime = newTime;
       window.setTimeout(function() {
                checkTime();
       },1000)
    }
        checkTime()
    

    1000ms = one second