added refresh interval to Bitcoin Price
This commit is contained in:
parent
a3a60d2c1d
commit
3dd177f0f3
@ -1,24 +1,35 @@
|
|||||||
// Fetch the Bitcoin price from the CoinGecko API
|
// Function to fetch and update the Bitcoin price
|
||||||
fetch('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd')
|
function updateBitcoinPrice() {
|
||||||
.then(response => response.json())
|
fetch('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd')
|
||||||
.then(data => {
|
.then(response => response.json())
|
||||||
const bitcoinPrice = data['bitcoin']['usd'];
|
.then(data => {
|
||||||
// Update the Bitcoin price on the website
|
const bitcoinPrice = data['bitcoin']['usd'];
|
||||||
const bitcoinPriceElement = document.getElementById('bitcoin-price');
|
// Update the Bitcoin price on the website
|
||||||
|
const bitcoinPriceElement = document.getElementById('bitcoin-price');
|
||||||
|
|
||||||
// Format the Bitcoin price with a comma after every three digits and no decimals
|
// Format the Bitcoin price with a comma after every three digits and no decimals
|
||||||
const formattedPrice = '$' + bitcoinPrice.toLocaleString(undefined, { minimumFractionDigits: 0, maximumFractionDigits: 0 });
|
const formattedPrice = '$' + bitcoinPrice.toLocaleString(undefined, { minimumFractionDigits: 0, maximumFractionDigits: 0 });
|
||||||
|
|
||||||
bitcoinPriceElement.textContent = formattedPrice; // Display the formatted Bitcoin price
|
bitcoinPriceElement.textContent = formattedPrice; // Display the formatted Bitcoin price
|
||||||
|
|
||||||
// Calculate dollars to sats
|
// Calculate dollars to sats
|
||||||
const dollarsToSatsElement = document.getElementById('dollars-to-sats');
|
const dollarsToSatsElement = document.getElementById('dollars-to-sats');
|
||||||
const satsConversionFactor = 100000000; // 1 bitcoin = 100 million sats
|
const satsConversionFactor = 100000000; // 1 bitcoin = 100 million sats
|
||||||
|
|
||||||
// Calculate how many sats you can get for 1 dollar based on the fetched Bitcoin price
|
// Calculate how many sats you can get for 1 dollar based on the fetched Bitcoin price
|
||||||
const dollars = 1;
|
const dollars = 1;
|
||||||
const sats = (1 / bitcoinPrice) * satsConversionFactor;
|
const sats = (1 / bitcoinPrice) * satsConversionFactor;
|
||||||
|
|
||||||
dollarsToSatsElement.textContent = `$1.00 = ${sats.toFixed(0)} sats`; // Display the result
|
dollarsToSatsElement.textContent = `$1.00 = ${sats.toFixed(0)} sats`; // Display the result
|
||||||
})
|
})
|
||||||
.catch(error => console.error(error));
|
.catch(error => console.error(error));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to refresh the Bitcoin price every 30 seconds
|
||||||
|
function refreshBitcoinPrice() {
|
||||||
|
updateBitcoinPrice(); // Call the function immediately to display the price on page load
|
||||||
|
setInterval(updateBitcoinPrice, 10000); // Call the function every 30 seconds (30,000 milliseconds)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call the function to refresh the Bitcoin price
|
||||||
|
refreshBitcoinPrice();
|
Loading…
Reference in New Issue
Block a user