There is a code that updates every 5 seconds. Div # result displays the cost value. How to make it so that if the value grows, the text becomes green, and if it drops, then it goes red.

var reloadData = function() { $.getJSON('https://api.cryptonator.com/api/ticker/ltc-usd', function(data){ var float = parseFloat(data.ticker.price).toFixed(2) $('#result').text(float) }) } setInterval('reloadData()', 5000) $(document).ready(reloadData) <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="result">Loading...</div> 

Example here

    1 answer 1

    You just have to compare the current value with the previous one and change the color value for the text depending on the result, something like this:

     var reloadData = function() { var float = Math.floor(Math.random() * 100) + 1; var prev = parseFloat($('#result').text()); var color = float >= prev ? 'green' : 'red'; $('#result').text(float).css('color', color); } setInterval(reloadData, 1000) 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="result">0</div>