-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbitstamp.js
More file actions
39 lines (34 loc) · 1.34 KB
/
Copy pathbitstamp.js
File metadata and controls
39 lines (34 loc) · 1.34 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
var previousTicker = { ask: 0, bid: 0, high: 0, last: 0, low: 0, volume: 0 }
setInterval(getTicker, 1000)
function getTicker() {
fetch('https://www.bitstamp.net/api/ticker/')
.then(response => {
if (!response.ok) {
throw new Error(response.status)
}
return response.json()
})
.then(ticker => {
for (key in ticker) {
const element = document.getElementById(key)
if (element == null) {
continue
}
// update HTML table
const amount = parseFloat(ticker[key])
const currency = key === 'volume' ? 'BTC' : 'USD'
element.textContent = new Intl.NumberFormat(navigator.language, { style: 'currency', currency, currencyDisplay: 'narrowSymbol' })
.format(amount)
.replace('BTC', '₿')
// update text color
if (ticker[key] > previousTicker[key]) {
element.style.color = 'green'
} else if (ticker[key] < previousTicker[key]) {
element.style.color = 'red'
} else {
element.style.color = 'black'
}
}
previousTicker = ticker
})
}