-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtimer.html
More file actions
75 lines (61 loc) · 1.79 KB
/
Copy pathtimer.html
File metadata and controls
75 lines (61 loc) · 1.79 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
<div class="container">
<label for="progress">Elapsed Time:</label>
<progress id="progress" max="100" value="0"></progress>
<time id="time">0s</time>
<label for="slider">Duration:</label>
<input type="range" id="slider" min="0" max="100">
<button id="reset">Reset</button>
</div>
<script type="importmap">
{
"imports": {
"rescript/": "/node_modules/rescript/",
"@rescript/": "/node_modules/@rescript/"
}
}
</script>
<script type="module">
import { Timer } from './timer.res.js';
let $slider = document.getElementById('slider');
let $time = document.getElementById('time');
let $progress = document.getElementById('progress');
let $reset = document.getElementById('reset');
let program = Timer.make(bind);
Timer.init(program);
function bind(state) {
let totalElapsedTime = Timer.State.getTotalElapsedTime(state);
let duration = Timer.State.getDuration(state);
let canTick = Timer.State.canTick(state);
$progress.value = Timer.Util.durationToPercent(totalElapsedTime);
$slider.value = Timer.Util.durationToPercent(duration);
$time.innerText = Timer.Util.formatTime(totalElapsedTime);
let ticker = null;
let tick = () => {
program = Timer.tick(program, Date.now());
};
if (canTick && !ticker) {
ticker = window.setInterval(tick, 100);
}
if (!canTick && ticker) {
window.clearInterval(ticker, tick)
ticker = null;
}
}
$slider.oninput = e => {
let duration = Timer.Util.percentToDuration(e.target.value);
program = Timer.updateDuration(program, Date.now(), duration);
}
$reset.onclick = () => {
program = Timer.reset(program, Date.now());
}
</script>
<style>
.container {
display: grid;
gap: 0.5rem;
max-width: 20rem;
}
progress {
width: 100%;
}
</style>