app js working fully"
This commit is contained in:
parent
3b142d20a4
commit
33f3607c20
@ -15,15 +15,58 @@ function updateBitcoinPrice(selectedCurrency) {
|
||||
})
|
||||
.catch(error => console.error(error));
|
||||
}
|
||||
//function refreshBitcoinPrice(selectedCurrency) {
|
||||
// updateBitcoinPrice(selectedCurrency);
|
||||
// setInterval(() => updateBitcoinPrice(selectedCurrency), 300000);
|
||||
//}
|
||||
|
||||
updateBitcoinPrice('usd');
|
||||
function updateSatsToDollarsConversion() {
|
||||
const satsInputElement = document.getElementById('sats-input');
|
||||
const dollarsOutputElement = document.getElementById('dollars-output');
|
||||
|
||||
const satsValue = parseFloat(satsInputElement.value);
|
||||
|
||||
if (!isNaN(satsValue)) {
|
||||
const currencySelect = document.getElementById('currency-select');
|
||||
const selectedCurrency = currencySelect.value;
|
||||
|
||||
fetch(`https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=${selectedCurrency}`)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
const bitcoinPrice = data['bitcoin'][selectedCurrency];
|
||||
const satsConversionFactor = 100000000; // 1 bitcoin = 100 million sats
|
||||
|
||||
const dollarsValue = (satsValue / satsConversionFactor) * bitcoinPrice;
|
||||
|
||||
dollarsOutputElement.textContent = `${dollarsValue.toFixed(2)} ${selectedCurrency.toUpperCase()}`;
|
||||
})
|
||||
.catch(error => console.error(error));
|
||||
} else {
|
||||
dollarsOutputElement.textContent = '';
|
||||
}
|
||||
}
|
||||
|
||||
function updateFiatToSatsConversion(selectedCurrency) {
|
||||
fetch(`https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=${selectedCurrency}`)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
const bitcoinPrice = data['bitcoin'][selectedCurrency];
|
||||
const satsConversionFactor = 100000000;
|
||||
const currencyValue = 1;
|
||||
const sats = (currencyValue / bitcoinPrice) * satsConversionFactor;
|
||||
const currencyToSatsElement = document.getElementById('dollars-to-sats');
|
||||
currencyToSatsElement.textContent = `1 ${selectedCurrency.toUpperCase()} = ${sats.toFixed(0)} sats`;
|
||||
})
|
||||
.catch(error => console.error(error));
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const satsInputElement = document.getElementById('sats-input');
|
||||
const currencySelect = document.getElementById('currency-select');
|
||||
|
||||
const selectedCurrency = currencySelect.value;
|
||||
// Initially fetch Bitcoin price in USD
|
||||
updateBitcoinPrice('usd');
|
||||
updateBitcoinPrice(selectedCurrency);
|
||||
updateFiatToSatsConversion(selectedCurrency)
|
||||
|
||||
satsInputElement.addEventListener('input', updateSatsToDollarsConversion);
|
||||
|
||||
@ -32,8 +75,8 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
updateBitcoinPrice(selectedCurrency);
|
||||
updateFiatToSatsConversion(selectedCurrency);
|
||||
updateSatsToDollarsConversion(selectedCurrency);
|
||||
updateFiatToSatsConversion(selectedCurrency)
|
||||
});
|
||||
});
|
||||
|
||||
refreshBitcoinPrice('usd'); // Refresh Bitcoin price in USD
|
||||
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user