-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
75 lines (62 loc) · 1.84 KB
/
script.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
const ws = new WebSocket('wss://nc.ciner.com.tr/sub/dot');
const wrapper = document.getElementById('wrapper');
let arr = [];
ws.addEventListener('open', (event) => {
console.log(event);
console.log("bağlandı");
});
ws.addEventListener('close', (event) => {
console.log(event);
})
ws.addEventListener('message', (event) => {
const data = JSON.parse(event.data);
addData(data);
console.log(data);
});
ws.addEventListener('error', (error) => {
console.log("Error Accoured", error);
})
const addData = (data) => {
const index = arr.findIndex(a => a.SecuritySlug == data.SecuritySlug);
if (index === -1) {
arr.push(data);
}
else {
arr[index] = data;
}
createElem(index, data);
}
const createElem = (index, item) => {
if (index == -1) {
const elem = document.createElement("div");
elem.className = "col-md-6 col-sm-12 col-xs-12 col-lg-3 mb-4";
elem.innerHTML = createCard(item);
wrapper.appendChild(elem);
}
else {
const elem = wrapper.childNodes[index + 1];
elem.innerHTML = createCard(item);
elem.classList.add("deneme");
setTimeout(() => {
elem.classList.remove("deneme");
}, 700);
}
}
const createCard = (item) => {
return `
<div class="card">
<div class="card-body">
<div class="left">
<h5 class="card-title">${item.SecuritySlug.split('-').join(" ")}</h5>
<p class="card-text">
<span>${item.LastPrice}</span>
<b>${item.PercentChange}</b>
</p>
</div>
<div class="right">
${item.ChangeDirection == 1 ? '<i class="fa-solid fa-circle-arrow-up"></i>' : '<i class="fa-solid fa-circle-arrow-down"></i>'}
</div>
</div>
</div>
`
}