-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_final.js
92 lines (83 loc) · 2.45 KB
/
script_final.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
const logo = document.querySelector('#logo');
const idSel = document.querySelector('#idSel');
const valSel = document.querySelector('#valSel');
const price = document.querySelector('#price');
const hChange = document.querySelector('#hChange');
const dChange = document.querySelector('#dChange');
const call = document.querySelector('#call');
const ids = [
'Bitcoin',
'Ethereum',
'Ripple',
'Litecoin',
'Chainlink',
'Tether',
'IOTA',
'NEO',
];
const money = ['EUR', 'USD', 'CAD', 'AUD', 'RUB'];
var interval = setInterval(callApi, 1000 * 90);
//creating options
ids.forEach((id) => {
let opt = document.createElement('option');
opt.value = id.toLowerCase();
opt.innerText = id;
idSel.add(opt);
});
money.forEach((val) => {
let opt = document.createElement('option');
opt.value = val.toLowerCase();
opt.innerText = val;
valSel.add(opt);
});
//function
async function callApi() {
let id = idSel.value;
let val = valSel.value;
let api = `https://api.coingecko.com/api/v3/coins/${id}?tickers=false&community_data=false&developer_data=false&sparkline=false`;
let response = await fetch(api, {
method: 'GET',
headers: {
accept: 'application/json',
},
});
let result = await response.json();
console.log(result);
//changes
logo.src = result.image.small;
price.innerText = `${result.market_data.current_price[val]} ${String(
val
).toUpperCase()}`;
hChange.classList.remove('minus', 'plus');
dChange.classList.remove('minus', 'plus');
let hChangeText =
result.market_data.price_change_percentage_1h_in_currency[val];
let dChangeText =
result.market_data.price_change_percentage_24h_in_currency[val];
if (hChangeText < 0) {
hChange.innerText = `${hChangeText}%`;
hChange.classList.add('minus');
} else {
hChange.classList.add('plus');
hChange.innerText = `+${hChangeText}%`;
}
if (dChangeText < 0) {
dChange.innerText = `${dChangeText}%`;
dChange.classList.add('minus');
} else {
dChange.classList.add('plus');
dChange.innerText = `+${dChangeText}%`;
}
//reset interval
clearInterval(interval);
interval = setInterval(callApi, 1000 * 90);
}
callApi();
//event listeners
call.addEventListener('click', callApi);
call.addEventListener('mousedown', () => {
call.classList.toggle('clicked');
});
call.addEventListener('mouseup', () => {
call.classList.toggle('clicked');
});