There is code for only one div. I need to apply this function to other divas with different id.

I change the parameters in the function and nothing happens, only one of them works or not one at all. what to do

var price1; var reloadData = function() { $.getJSON('https://api.cryptonator.com/api/ticker/btc-usd', function(data){ var float1 = parseFloat(data.ticker.price).toFixed(2) if(price1 && price1 > float) { $('#result1').html('<font color="red">' + float1 + '</font>') } else if(price1 && price1 < float1) { $('#result1').html('<font color="green">' + float1 + '</font>') } else { $('#result1').text(float1) } price1 = float1 }) } setInterval('reloadData()', 5000) $(document).ready(reloadData) var price2; var reloadData = function() { $.getJSON('https://api.cryptonator.com/api/ticker/ltc-usd', function(data){ var float2 = parseFloat(data.ticker.price).toFixed(2) if(price2 && price2 > float2) { $('#result2').html('<font color="red">' + float2 + '</font>') } else if(price2 && price2 < float) { $('#result2').html('<font color="green">' + float2 + '</font>') } else { $('#result2').text(float2) } price2 = float2 }) } setInterval('reloadData()', 5000) $(document).ready(reloadData) <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="result1">Loading...</div> <div id="result2">Loading...</div> 

    3 answers 3

    Why clone something? do one function for all.

    Something like this :

     function reloadAll() { setInterval(function() { reloadData('result1', 'url 1'); }, 1000); setInterval(function() { reloadData('result2', 'url 2'); }, 1000); }; function reloadData(id, url) { // url - для разных дивов разные url var float = Math.floor(Math.random() * 100) + 1; var prev = parseFloat($('#' + id).text()); var color = float >= prev ? 'green' : 'red'; $('#' + id).text(float).css('color', color); }; $(document).ready(reloadAll); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="result1">Loading...</div> <div id="result2">Loading...</div> 

    PS: never do many functions that do the same thing, it is better to do 1, but more flexible, function.

    • So he has different api, as I understood. The first time is https://api.cryptonator.com/api/ticker/btc-usd , and the second one is https://api.cryptonator.com/api/ticker/ltc-usd Or can I make your example for two different ones? - Igor Lut
    • I even passed the url to the function ... - Rostyslav Kuzmovych
    • Sorry, did not notice. - Igor Lut
    • Nothing scary, we are all wrong)) thanks vseravno)) - Encode_VI

     let base = [ { id: 1, url: 'btc-usd' }, { id: 2, url: 'ltc-usd' } ]; setInterval(reloadData, 5000) $(document).ready(reloadData) function reloadData() { for(let i = 0; i < base.length; i++) { $.getJSON(`https://api.cryptonator.com/api/ticker/${base[i].url}`, function(data){ const $result = $(`#result${base[i].id}`), newPrice = parseFloat(data.ticker.price).toFixed(2); if(base[i].price && base[i].price !== newPrice) { $result.html(`<font color="${(base[i].price > newPrice ? 'red' : 'green')}">${newPrice}</font>`); } else { $result.text(newPrice) } base[i].price = newPrice; }) } } 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="result1">Loading...</div> <div id="result2">Loading...</div> 

      Try calling the second function not reloadData , but reloadData2 or a more comprehensible name. And then $(document).ready(reloadData2)

      • does not work (( - Encode_VI
      • @Encode_VI And setInterval('reloadData()', 5000) Also changed to reloadData2() ? - Igor Lut