Bitcoin Price that updates in specific Seconds

0 votes

I have with me my HTML and Java code which is used to retrieve and display the price of the Bitcoin in USD through the bitcoininfo. I want it to auto update on Javascript every 5 seconds. This ensures non refreshing of the page to get the current price. Have shared my code below:- 

<!DOCTYPE html> 
<html lang="en" > 
<head> 

  <meta charset="UTF-8"> 
  <title>Bitcoin Current price</title> 
  <style>#wrapper { 
  font-size: 1em; 
  font-family: arial; 
  margin: 20px auto; 
  width:450px; 
  color: green; 
  text-align: center; 
} 

#btc {font-size:6em;} 

</style> 
</head> 
<body> 
<!-- partial:index.partial.html --> 
<div id="wrapper"> 
    <h1>Bitcoin Current Price</h1> 
    <div id="btc"></div> 
    </div> 
    <!-- partial --> 
      <script> var currentPrice = new XMLHttpRequest(); 

currentPrice.open('GET', 'https://api.gdax.com/products/BTC-USD/book', true); currentPrice.onreadystatechange = function(){ 
    if(currentPrice.readyState == 4){ 
        var ticker = JSON.parse(currentPrice.responseText); 
        var price = ticker.bids[0][0]; 
        document.getElementById('btc').innerHTML = "$" + price; 
  }; 
}; 
currentPrice.send();</script> 

</body>
</html>

If there's a better way to do it, a pointer in the right direction will be well appreciated. Thank you!

Feb 28, 2022 in Blockchain by Aditya
• 7,680 points
330 views

1 answer to this question.

0 votes

The better way to do so is to make the API into a function by wrapping the call and then keep constantly calling it every 5 seconds by using setInterval. Have shared the code below:-


<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Bitcoin Current price</title> 
    <style> 
        #wrapper { 
            font-size: 1em; 
            font-family: arial; 
            margin: 20px auto; 
            width: 450px; 
            color: green; 
            text-align: center; 
} 

#btc { 
    font-size: 6em; 
} 
  </style> 
</head> 
<body> 
    <!-- partial:index.partial.html --> 
    <div id="wrapper"> 
          <h1>Bitcoin Current Price</h1> 
        <div id="btc"></div> 
</div> 
<!-- partial --> 
<script> 
  function get_price() { 
  var el = document.getElementById('btc') 
  fetch("https://api.gdax.com/products/BTC-USD/book") 
  .then(res => res.json()) .then(res => { 
  var price = res.bids[0][0]; 
    el.innerHTML = "$" + price; 
}).catch(err => { 
    el.innerHTML = "$0.00 - Error"; 
}); 

}

get_price() 
setInterval(get_price, 5000) 
</script> 

</body> 

</html>

 

answered Feb 28, 2022 by Soham
• 9,700 points

Related Questions In Blockchain

0 votes
1 answer

How to use bitcoin price as place holder in php?

You can do it by providing an ...READ MORE

answered Aug 21, 2018 in Blockchain by digger
• 26,740 points
529 views
+1 vote
1 answer

Is there a way to send funds from a specific bitcoin address in a wallet?

I'll answer my own question, It was unbelievably ...READ MORE

answered Sep 3, 2018 in Blockchain by Perry
• 17,100 points
1,240 views
+1 vote
1 answer
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,450 points
1,142 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,690 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,231 views
0 votes
1 answer

Bitcoin Core on external SSD in OS X

 In order to do so, assuming you ...READ MORE

answered Feb 28, 2022 in Blockchain by Soham
• 9,700 points
1,131 views
0 votes
1 answer

JAVA Retrive current bitcoin price with JSON

You should start log Exceptions when you ...READ MORE

answered Feb 28, 2022 in Blockchain by Soham
• 9,700 points
792 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP