I am writing an app running inside a browser. Every minute I load a json file from my server with current data and need compare these data with data from the previously loaded json file. If there is any change, I will show new data dynamically inside the app.
I have used a function with a delay for it. Example at the JSfiddle. However, I am not able to get local variables from the function with the delay into next function, in which I would be able to compare the data.
I have tried create global variables, tried work with scope variables, and even save values in storage. Nothing has been working for me. Output from the JSON file are objects.
Any help, please? JSfiddle: https://jsfiddle.net/vladoss/r02aLv60/2/
dataUpdates();
var ch1;
function dataUpdates() {
ch = (function() {
ch = null;
$.ajax({
'async': false,
'global': false,
'url': "json.json", //non-exisitng json.
'dataType': "json",
'success': function (data) {
ch = data;
}
});
return ch; //current data
})();
setTimeout(oldValues, 3000);
function oldValues() {
ch1 = ch[0]; //old data
}
if (ch1 !== ch[0]) { //Here I need be able to read variable ch1.
console.log(ch1); //ch1 is undefined
//...
}
}
setInterval(dataUpdates, 5000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>