I created variable bpJson, and am not understanding why it is changing. I am console logging it every 5 seconds, and it changes each iteration. I only expected babyPieData to change while bpJson stays the same each time. There is no bpJson in setPieOptions so I did not include the code for that function.
var createVariance = function(bpJson, babyPieData){
var n = bpJson.length;
for ( var i = 0; i < n; i++){
var amount = Math.floor(Math.random() * 4);
var operator = Math.floor(Math.random() *2) + 1;
if (operator === 1){
babyPieData[i] = (bpJson[i] + amount);
if (babyPieData[i] > 100){
babyPieData[i] = 100;
}
}else{
babyPieData[i] = (bpJson[i] - amount);
if (babyPieData[i] < 0){
babyPieData[i] = 1;
}
}
setPieOptions(babyPieData);
}
};
var getBabyPieData = function(){
var xhr = new XMLHttpRequest();
xhr.open('GET', 'php/baby-pie.php');
xhr.send();
xhr.onload = function(){
var babyPieData = JSON.parse(xhr.response);
console.log('original babyPieData = ' + babyPieData);
var bpJson = babyPieData;
setPieOptions(babyPieData);
var babyPieDataTimer = window.setInterval(function(){
createVariance(bpJson, babyPieData);
console.log(bpJson);
}, 5000);
};
}();