0

I am trying to update chart through comparing two arrays in realtime. I noticed the if statement only run once in setInterval or setTimeout. It will run the condition the first time it is reached and can't change anymore. It seems to work fine above if statement in the console. How do I solve this problem?

var datar = [{"Dcha":"57","Bcha":"88","TIME":"03:53:00"},{"Dcha":"47","Bcha":"68","TIME":"03:53:00"},{"Dcha":"47","Bcha":"68","TIME":"03:51:00"},{"Dcha":"45","Bcha":"80","TIME":"03:25:00"}]

function Update() {
  $.ajax({
    url: url,
    type: "GET",
    success: function(data) {
      var datax = JSON.parse(data);
      var datay = datax.reverse();
      var a1 = JSON.stringify(datay, Object.keys(datay).sort());
      var a2 = JSON.stringify(datar, Object.keys(datar).sort());
      console.log(datay);
      console.log(datar); //To here is working fine
      if (a1 == a2) {
        getdatar(datay);
        var month = MONTHS[TIME.length % MONTHS.length];\\
        TEST CODE, this will add value when two array are same;
        TIME.push(month);
        Dcha.push(randomScalingFactor());
        Bcha.push(randomScalingFactor());
        window.dchart.update();
      }
    }
  });
};
setInterval(Update, 2000);
Barmar
  • 741,623
  • 53
  • 500
  • 612
air1007
  • 1
  • 1
  • 2
    could you explain what is in `a1` and `a2` ? – Nicolas Nov 26 '19 at 19:55
  • 2
    I can assure you that the setTimeout is being called. What you should know is that you're doing `string` comparison on a JSON object. It might be better to look into what those object actually are (a1 and a2) and use some different method to compare equality – dev-cyprium Nov 26 '19 at 19:59
  • Compare two json array need to use JSON.stringify() let them back to JSON. Some Tutorial use this. – air1007 Nov 26 '19 at 20:01
  • The `if` statement will always run; whether the condition evaluates to true or false will always depend on what the service returns, and whether reversing the array causes the JSON serialization routine to produce a different result. One would expect that with anything other than bizarrely palindromic data that would always be the case, but I'm not clear on what the serialization logic is doing. – Heretic Monkey Nov 26 '19 at 20:01
  • In fact, when I run `var datar = [{"Dcha":"57","Bcha":"88","TIME":"03:53:00"},{"Dcha":"47","Bcha":"68","TIME":"03:53:00"},{"Dcha":"47","Bcha":"68","TIME":"03:51:00"},{"Dcha":"45","Bcha":"80","TIME":"03:25:00"}]; JSON.stringify(datar, Object.keys(datar).sort());` in a console, I get `"[{},{},{},{}]"`, which isn't very illuminating... – Heretic Monkey Nov 26 '19 at 20:04
  • Think that's I made all mistake in beginning, Thanks to everyone who answered me. – air1007 Nov 26 '19 at 20:13

1 Answers1

1

You can't compare json in javascript that way. Object comparison or json in this case isn't just by == or ===.

Try using Lodash._isEqual(json1, json2) as described here: https://stackoverflow.com/a/26049421/9347459

Tolumide
  • 944
  • 9
  • 11