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