main.pug
1.29 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
extends layout
block content
.timeline
h2 거래 진행 목록
table#good-list
tr
th 상품명
th 이미지
th 시작 가격
th 남은 시간
th 입장
for good in goods
tr
td= good.name
td: img(src='/img/' + good.img)
td= good.price
td.time(data-start=good.createdAt) 00:00:00
td: a.enter.btn(href='/good/' + good.id) 입장
script(src='https://cdnjs.cloudflare.com/ajax/libs/event-source-polyfill/0.0.9/eventsource.min.js')
script.
var es = new EventSource('/sse');
es.onmessage = function (e) {
document.querySelectorAll('.time').forEach(function(td) {
var end = new Date(td.dataset.start);
var server = new Date(parseInt(e.data, 10));
end.setDate(end.getDate() + 1);
if (server >= end) {
return td.textContent = '00:00:00';
} else {
var t = end - server;
var seconds = ('0' + Math.floor((t / 1000) % 60)).slice(-2);
var minutes = ('0' + Math.floor((t / 1000 / 60) % 60)).slice(-2);
var hours = ('0' + Math.floor((t / (1000 * 60 * 60)) % 24)).slice(-2);
return td.textContent = hours + ':' + minutes + ':' + seconds ;
}
});
};