-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoscowTime.js
107 lines (88 loc) · 3.77 KB
/
MoscowTime.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
document.addEventListener('DOMContentLoaded', function() {
let sintraWebSocket = new WebSocket('wss://api.sintra.fi/ws');
sintraWebSocket.onopen = function() {
console.log("Connected to Sintra WebSocket");
};
sintraWebSocket.onmessage = function(event) {
const data = JSON.parse(event.data);
if (data.event === "data") {
const btcPrice = parseFloat(data.data.prices.usd);
if (!isNaN(btcPrice)) {
console.log(`Received BTC price from Sintra: ${btcPrice}`);
const satoshisPerDollar = 1 / (btcPrice / 100000000);
console.log(`Received satoshis per USD: ${satoshisPerDollar}`);
let satoshisText = document.getElementById('satoshisValue').textContent;
let oldSatoshisPerDollar = parseInt(satoshisText, 10);
if (isNaN(oldSatoshisPerDollar)) {
console.error('Old satoshis per dollar value is not a number:', satoshisText);
oldSatoshisPerDollar = 1000; // Use `let` for variables that may need to be reassigned
}
animateNumberChange('satoshisValue', oldSatoshisPerDollar, Math.round(satoshisPerDollar), 500);
} else {
console.error("Invalid BTC price received:", data.data.prices.usd);
}
}
};
sintraWebSocket.onerror = function(error) {
console.error("WebSocket Error: ", error);
};
sintraWebSocket.onclose = function(event) {
if (!event.wasClean) {
console.log("WebSocket Connection Closed Unexpectedly; attempting to reconnect...");
setTimeout(() => {
sintraWebSocket = new WebSocket('wss://api.sintra.fi/ws'); // Correct re-initialization inside setTimeout
}, 5000);
}
};
function animateNumberChange(elementId, start, end, duration) {
let current = start;
const range = end - start;
const increment = end > start ? 1 : -1;
const stepTime = Math.abs(Math.floor(duration / Math.abs(range)));
const obj = document.getElementById(elementId);
if (range === 0) {
obj.textContent = end; // If no change needed, just set the text
return;
}
const timer = setInterval(() => {
current += increment;
obj.textContent = current;
if (current === end) {
clearInterval(timer);
}
}, stepTime);
}
});
// Handle opening the Moscow Time modal
function openMoscowTimeModal(event) {
event.stopPropagation(); // Stop the click from propagating to more general handlers
const moscowModal = document.getElementById('moscowTimeModal');
if (moscowModal) {
closeAllModals(); // Close all other modals
moscowModal.classList.add('active');
document.body.classList.add('modal-open');
} else {
console.error('Moscow Time Modal not found!');
}
}
// Function to close the Moscow Time modal
function closeMoscowTimeModal() {
const moscowModal = document.getElementById('moscowTimeModal');
if (moscowModal) {
moscowModal.classList.remove('active');
document.body.classList.remove('modal-open');
} else {
console.error('Moscow Time Modal not found!');
}
}
// element to trigger opening the Moscow Time modal
const moscowTrigger = document.querySelector('.info-modal-trigger');
if (moscowTrigger) {
moscowTrigger.addEventListener('click', openMoscowTimeModal);
}
document.querySelector('#moscowTimeModal .close').addEventListener('click', function() {
closeMoscowTimeModal();
});
document.querySelector('.info-modal-trigger').addEventListener('click', function() {
showModal('moscowTimeModal'); // Pass the ID of the modal if needed
});