Repository navigation
Expand file tree
/
Copy pathtimer.js
More file actions
96 lines (77 loc) · 2.39 KB
/
Copy pathtimer.js
File metadata and controls
96 lines (77 loc) · 2.39 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'use strict';
var Timer = {
get actionButton() {
delete this.actionButton;
return this.actionButton = document.getElementById('timer-action-button');
},
get tickerView() {
delete this.tickerView;
return this.tickerView = document.getElementById('timer-ticker-view');
},
get chronoView() {
delete this.chronoView;
return this.chronoView = document.getElementById('timer-chrono-view');
},
get durationField() {
delete this.durationField;
return this.durationField = document.getElementById('duration-field');
},
execute: function ti_execute(action) {
if (!this[action]) {
return;
}
this[action]();
},
start: function ti_start() {
if (!this.durationField.validity.valid) {
return;
}
this.actionButton.dataset.action = 'cancel';
this.chronoView.parentNode.classList.remove('ended');
this.tickerView.classList.add('running');
this.durationField.disabled = true;
var duration = this.duration(this.durationField.value);
var endTime = Date.now() + duration;
this.updateChrono(duration);
this._ticker = setInterval(function ti_updateChrono(self) {
var remaining = endTime - Date.now();
if (remaining <= 0) {
self.updateChrono(0);
self.end();
return;
}
self.updateChrono(remaining);
}, 500, this);
},
cancel: function ti_cancel() {
this.actionButton.dataset.action = 'start';
this.tickerView.classList.remove('running');
this.durationField.disabled = false;
this.updateChrono(0);
clearInterval(this._ticker);
delete this._ticker;
},
end: function ti_end() {
//TODO: ring too
try {
navigator.mozVibrate([200, 200, 200, 200, 200]);
} catch (e) {}
this.cancel();
this.chronoView.parentNode.classList.add('ended');
},
updateChrono: function ti_updateChrono(remaining) {
var currentValue = this.chronoView.innerHTML;
var newValue = new Date(remaining).toLocaleFormat('%M:%S');
if (currentValue != newValue)
this.chronoView.innerHTML = newValue;
},
duration: function ti_duration(value) {
var durationComponents = value.split(':');
var duration = 0;
for (var i = 0; i < durationComponents.length; i++) {
var unitHandler = Math.pow(60, durationComponents.length - 1 - i);
duration += unitHandler * 1000 * durationComponents[i];
}
return duration;
}
};