I am new to JavaScript and back-end coding as well. I need help with initiating counter. The code I have written below does the following things.
- Loop through an external JSON file that has set of objects (numbers) and compares its previous value to the current value
- If the current value is less than or greater than the previous value, it changes the image
What I want to do is, when the current value is less than or greater than the previous value, it should change the background color and start a counter called counterAlert. This counter should count up to 10 seconds and change the background color back to the original color but, during this 10 second time frame, the function should continue to loop through the JSON file and if it hits the condition where current value is again, greater than or less than the previous value, the counter should reset.
JavaScript code I have to loop through the JSON file is the following
var counter = 0; // Trial
var counterAlert = 0
$(document).ready(function () {
$.ajaxSetup({ cache: false });
myJson();
});
// Below is the function to draw data from Json
function myJson() {
$.getJSON("Janus.json", function (response) {
var i = 1380; //start demo at last hour of data.
var looping = setInterval(function () {
var TrialCount = response.length;
var Info = response[counter];
var Checker_Data = Info.WOB;
Checker_Data = Checker_Data.toFixed(2);
CompareChecker();
function CompareChecker() {
if (counter == 0) {
prev_Checker_Data = Checker_Data;
}
if (Checker_Data > prev_Checker_Data) {
if ((Math.abs(Checker_Data - prev_Checker_Data) >= prev_Checker_Data / 2 ))
document.getElementById("Checker_img").src = "img/yeltri.png",
document.getElementById("CheckerValue").style.backgroundColor = '#006699';
else if ((Math.abs(Checker_Data - prev_Checker_Data) <= prev_Checker_Data / 2))
document.getElementById("Checker_img").src = "img/grnsqr.png";
}
else if (Checker_Data < prev_Checker_Data) {
if ((Math.abs(Checker_Data - prev_Checker_Data) >= prev_Checker_Data / 2))
document.getElementById("Checker_img").src = "img/yeltriDn.png";
else if ((Math.abs(Checker_Data - prev_Checker_Data) <= prev_Checker_Data / 2))
document.getElementById("Checker_img").src = "img/grnsqr.png";
}
prev_Checker_Data = Checker_Data;
}
document.getElementById("Checker").innerHTML = Checker_Data
counter++;
if (counter == TrialCount) clearInterval(looping);
}, 10);
});
};
I am thinking, to do what I want, I need to insert another function under the function CompareChecker()'s If statement such that
if (Checker_Data > prev_Checker_Data) {
if ((Math.abs(Checker_Data - prev_Checker_Data) >= prev_Checker_Data / 2 ))
document.getElementById("Checker_img").src = "img/yeltri.png",
document.getElementById("CheckerValue").style.backgroundColor = '#006699',
checkerValue_Timer();
function checkerValue_Timer(){
counterAlert++;
if (counterAlert == 10)
document.getElementById("CheckerValue").style.backgroundColor = '#cecece';
}
setInterval(CheckerValue_Timer, 1000);
but I am not sure, please help