-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
82 lines (66 loc) · 2.91 KB
/
index.js
File metadata and controls
82 lines (66 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Global variable to store the currently selected currency
let currentCurrency = "USD";
async function fetchBitcoinPrice() {
// Construct the API URL with the current currency
const apiUrl = `https://api.coinbase.com/v2/prices/BTC-${currentCurrency}/spot`;
try {
// Fetch data from the API
let response = await fetch(apiUrl);
// Parse the JSON response
let json = await response.json();
// Extract the Bitcoin price from the response
const bitcoinPrice = json.data.amount;
// Update the price display on the webpage
document.getElementById("price").textContent = `${currentCurrency} $${bitcoinPrice}`;
} catch (error) {
// Log any errors that occur during the fetch operation
console.error("Error fetching Bitcoin price:", error);
}
}
function togglePriceVisibility() {
// Get the element that displays the price
const priceElement = document.getElementById("price");
// Toggle between hiding and showing the price
if (priceElement.style.display === "none") {
priceElement.style.display = "inline";
} else {
priceElement.style.display = "none";
}
}
function updateDateTime() {
// Get the element where the date/time will be displayed
const dateTimeElement = document.getElementById("datetime");
// Get the current date and time as a localized string
const currentDateTime = new Date().toLocaleString();
// Update the date/time display
dateTimeElement.textContent = currentDateTime;
}
// Wait for the DOM to be fully loaded before setting up event listeners
document.addEventListener("DOMContentLoaded", function() {
setInterval(updateDateTime, 1000); // Update date/time every second
// Initial calls to display data immediately
updateDateTime();
});
function handleCurrencyChange(currency){
}
// Wait for the DOM to be fully loaded before setting up event listeners
document.addEventListener("DOMContentLoaded", function() {
// Get references to the relevant DOM elements
const currencySelector = document.getElementById("currency-selector");
const toggleButton = document.getElementById("toggle-button");
// Set up event listener for currency selection changes
currencySelector.addEventListener("change", function () {
// Update the current currency when the selection changes
currentCurrency = this.value;
// Fetch and display the price for the new currency
fetchBitcoinPrice();
});
// Set up event listener for the toggle visibility button
toggleButton.addEventListener("click", togglePriceVisibility);
// Set up intervals for automatic updates
setInterval(fetchBitcoinPrice, 3000); // Update price every 3 seconds
setInterval(updateDateTime, 1000); // Update date/time every second
// Initial calls to display data immediately
fetchBitcoinPrice();
updateDateTime();
});