-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstopwatch.html
More file actions
72 lines (71 loc) · 1.96 KB
/
stopwatch.html
File metadata and controls
72 lines (71 loc) · 1.96 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
<!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 - The Merlin JS framework</title>
</head>
<body>
<main>
<h1><img src="../favicon.ico"> Stopwatch</h1>
<h2 text:="clock">0.000</h2>
<button onclick:="start">Start</button>
<button onclick:="stop">Stop</button>
<button onclick:="reset">Reset</button>
</main>
<script type="module">
import {app} from "../index.js"
app({
node: document.body.querySelector('main'),
init: {
clock: 0,
offset: 0
},
register: (update, dispatch) => ({
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}, events) => ({
clock: clock.toFixed(3),
...events
})
})
</script>
</body>
</html>