简体   繁体   English

如何检查JSON中的字符串是否已使用Javascript更改

[英]How can I check if a string in JSON has changed with Javascript

I am trying to check if a string/date in a JSON file has changed but checking every second. 我正在尝试检查JSON文件中的字符串/日期是否已更改,但每秒检查一次。 How can I store this value and check the file for change or difference? 如何存储该值并检查文件中的更改或差异? Any help much appreciated. 任何帮助,不胜感激。

// json example:
{
  "LastControlUpdateTime": "10:43:06.7147187"
}

var timeStamp;
function CompareTime() {
  $.getJSON(playlistJSONPath, function (data) {
    timeStamp = data.LastControlUpdateTime;

    if (timeStamp === data.LastControlUpdateTime) {
      console.log("same");
    } else {
      console.log("Diff");
    }
  });
}

CompareTime();
setInterval(CompareTime, 1000);

you are comparing the just assigned variable. 您正在比较刚分配的变量。 this should do it. 这应该做到。

var old_value;
function CompareTime() {
  $.getJSON(playlistJSONPath, function (data) {
    if (old_value == undefined) {
     old_value = data.LastControlUpdateTime;
    }
    else {
      if (data.LastControlUpdateTime != old_value) {
        console.log("value changed");
        old_value = data.LastControlUpdateTime;
      }
      else {
        console.log("value did not change");
      }
    }
  });
  setTimeout(function() { CompareTime();}, 1000)
}
CompareTime();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM