I utilized an API that supplies me with information in JSON format, retrieved the price of a specific currency, and presented it on a screen using JavaScript. I encapsulated this process within a function that dynamically updates the information at set intervals.
Now, I am looking for a piece of code that can compare the current price value against the next value received. If the new price is higher than the previous one, I want to either change the background color to green or trigger an alert. Conversely, if the new price is lower than the previous one, I would like the background color to switch to red.
Here's the snippet from my existing code:
var auto_refresh = setInterval(
function() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseText);
document.getElementById("bids1").innerHTML = myObj.BTCUSDT['bids']['0']['0'];
}
};
xmlhttp.open("GET", "all.json", true);
xmlhttp.send();
}, 1000);