/
timer.html
136 lines (122 loc) · 3.68 KB
/
timer.html
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<!DOCTYPE html>
<html>
<head>
<title>Pushups Timer</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flux/2.1.1/Flux.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mithril/0.2.0/mithril.js"></script>
<script src="https://cdn.rawgit.com/insin/msx/master/dist/MSXTransformer.js"></script>
<style>
body {
margin: 0
}
#timer {
font-family: sans-serif;
font-weight: bold;
font-size: 36vw;
margin: 0;
padding: 0;
text-align: center;
width: 100%;
}
.time-minimum {
background-color: green;
}
.time-warning {
background-color: yellow;
}
.time-maximum {
background-color: red;
}
</style>
</head>
<body>
<div id="app">
<div id="timer">20:00</div>
</div>
<script type="text/msx;harmony=true">
'use strict';
var TimerApp = {
controller: function() {
var ctrl = this;
ctrl.startTime = (new Date()).getTime();
ctrl.previous = 0;
ctrl.elapsed = 0;
ctrl.minimum = 30;
ctrl.warning = 50;
ctrl.maximum = 60;
ctrl.status = 'time-early';
function tick() {
m.startComputation();
ctrl.elapsed = ((new Date()).getTime() - ctrl.startTime) / 1000 + ctrl.previous;
if (ctrl.elapsed < ctrl.minimum) {
ctrl.status = 'time-early';
};
if (ctrl.minimum <= ctrl.elapsed && ctrl.elapsed < ctrl.warning) {
ctrl.status = 'time-minimum';
}
if (ctrl.warning <= ctrl.elapsed && ctrl.elapsed < ctrl.maximum) {
ctrl.status = 'time-warning';
}
if (ctrl.elapsed >= ctrl.maximum) {
ctrl.status = 'time-maximum';
}
m.endComputation();
}
ctrl.start = function() {
ctrl.startTime = (new Date()).getTime();
ctrl.ticker = setInterval(tick, 100);
};
ctrl.stop = function() {
tick();
if (ctrl.ticker) {
clearInterval(ctrl.ticker);
ctrl.ticker = false;
}
ctrl.previous = ctrl.elapsed;
ctrl.ticker = false;
};
ctrl.startstop = function() {
if (ctrl.ticker) {
ctrl.stop();
} else {
ctrl.start();
}
};
ctrl.reset = function() {
ctrl.stop();
ctrl.elapsed = 0;
ctrl.previous = 0;
ctrl.status = 'time-early';
};
function twoDigit(num) {
return num < 10 ? '0' + num : num;
}
ctrl.display = function() {
return Math.floor(ctrl.elapsed/60) + ':' + twoDigit(Math.floor(ctrl.elapsed % 60));
}
function saveBattery() {
if (document.hidden) {
if (ctrl.ticker) {
clearInterval(ctrl.ticker);
ctrl.ticker = false;
}
} else {
ctrl.ticker = setInterval(tick, 1000);
}
}
document.addEventListener('visibilitychange', saveBattery);
ctrl.onunload = function() {
document.removeEventListener('visibilitychange', saveBattery);
}
},
view: function(ctrl) {
return <div>
<div id="timer" className={ctrl.status}>{ctrl.display()}</div>
<p>
<button onclick={ctrl.startstop}>Start/Stop</button>
<button onclick={ctrl.reset}>Reset</button>
</p>
</div>;
}
};
m.mount(document.getElementById('app'), TimerApp);