-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstopwatch.html
More file actions
78 lines (77 loc) · 2.19 KB
/
stopwatch.html
File metadata and controls
78 lines (77 loc) · 2.19 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" type="image/x-icon" href="../favicon.ico">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
<title>Stopwatch - Ring</title>
</head>
<body>
<main>
<h1><img src="../favicon.ico"> Stopwatch</h1>
<h2></h2>
<button class="start">Start</button>
<button class="stop">Stop</button>
<button class="reset">Reset</button>
<button onclick="kill()">Kill</button>
</main>
<script type="module">
import ring from "../index.js"
window.kill = ring({
init: {
clock: 0,
offset: 0
},
register: (update, dispatch) => ({
init: () => {
document.body.querySelectorAll('button[class]').forEach(btn => {
btn.addEventListener('click', () => {
dispatch(btn.getAttribute('class'))
})
})
},
reset: () => update(state => ({
...state,
clock: 0
})),
start: () => {
var tick
update(state => {
tick = !state.offset
return {
...state,
offset: Date.now()
}
})
if (tick) {
dispatch('tick')
}
},
stop: () => update(state => ({
...state,
offset: 0
})),
tick: () => {
update(({offset, clock}) => {
if (offset) {
const now = Date.now()
setTimeout(() => dispatch('tick'), 100)
return {
clock: clock + (now - offset) / 1000,
offset: now
}
} else {
return {offset, clock}
}
})
}
}),
view: ({clock}, dispatch) => {
document.body.querySelector('h2').textContent = clock.toFixed(3)
}
})
</script>
</body>
</html>