-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathup.js
More file actions
82 lines (65 loc) · 2.41 KB
/
Copy pathup.js
File metadata and controls
82 lines (65 loc) · 2.41 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
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
const $at = document.getElementById('at')
const $d = document.getElementById('d')
const $h = document.getElementById('h')
const $m = document.getElementById('m')
const $s = document.getElementById('s')
document.fonts.ready.then(function () {
document.documentElement.classList.remove('loading')
fetch('up.json')
.then(function (response) { return response.json() })
.then(function (data) {
const atDate = new Date(data.at)
const at = atDate.toLocaleString(undefined, { dateStyle: 'long', timeStyle: 'short' })
const atFull = atDate.toLocaleString(undefined, { dateStyle: 'full', timeStyle: 'full' })
format(data.up)
$at.textContent = at
$at.dateTime = data.at
$at.title = atFull
const up = toSeconds(data.up)
const elapsed = Math.round((new Date() - atDate) / 1000)
const estimation = up + elapsed
const minRate = 50
const multiplier = .5
const rate = minRate + (elapsed * multiplier)
const duration = (estimation - up) / rate * 1000
const start = performance.now()
function frame(timestamp) {
const progress = Math.min((timestamp - start) / duration, 1)
const current = Math.round(up + (estimation - up) * progress)
update(current)
if (progress < 1) {
requestAnimationFrame(frame)
} else {
const liveStart = new Date()
setInterval(function () {
const liveElapsed = Math.round((new Date() - liveStart) / 1000)
update(estimation + liveElapsed)
}, 1000)
}
}
requestAnimationFrame(frame)
})
})
function toSeconds(hms) {
const [s, m, h] = hms.split(':').map(Number).reverse()
return s + (m * 60) + (h * 3600)
}
function toHMS(seconds) {
const h = Math.floor(seconds / 3600)
const m = Math.floor((seconds % 3600) / 60)
const s = seconds % 60
return `${h}:${m}:${s}`
}
function format(hms) {
const [seconds, minutes, h] = hms.split(':').reverse()
const days = Math.floor(h / 24)
const hours = h % 24
document.title = 'uptime: ' + new Intl.DurationFormat(undefined, { style: 'narrow' }).format({ days, hours, minutes })
$d.textContent = new Intl.DurationFormat().format({ days }) || '\u00A0'
$h.textContent = String(hours).padStart(2, '0')
$m.textContent = String(minutes).padStart(2, '0')
$s.textContent = String(seconds).padStart(2, '0')
}
function update(seconds) {
format(toHMS(seconds))
}