-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathexample.stopwatch.ts
119 lines (92 loc) · 2.33 KB
/
example.stopwatch.ts
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
import { EventList, IEvent } from "strongly-typed-events";
class Stopwatch {
private _events = new EventList<Stopwatch, StopwatchEventArgs>();
private _ticks = 0;
private _timer: number;
get onStart(): IEvent<Stopwatch, StopwatchEventArgs> {
return this._events.get("onStart").asEvent();
}
get onPause(): IEvent<Stopwatch, StopwatchEventArgs> {
return this._events.get("onPause").asEvent();
}
get onReset(): IEvent<Stopwatch, StopwatchEventArgs> {
return this._events.get("onReset").asEvent();
}
private dispatch(name: string) {
this._events
.get(name)
.dispatch(this, new StopwatchEventArgs(this._ticks, this.display()));
}
start() {
if (this._timer == null) {
this._timer = Date.now();
this.dispatch("onStart");
}
}
pause() {
if (this._timer) {
this._ticks = this.getTicks();
this._timer = null;
this.dispatch("onPause");
}
}
reset() {
this._ticks = 0;
this._timer = Date.now();
this.dispatch("onReset");
}
getTicks() {
if (this._timer) {
return Date.now() - this._timer + this._ticks;
}
return this._ticks;
}
display() {
var ticks = this.getTicks();
//get seconds from ticks
var ts = ticks / 1000;
//conversion based on seconds
let hh: any = Math.floor(ts / 3600);
let mm: any = Math.floor((ts % 3600) / 60);
let ss: any = (ts % 3600) % 60;
//prepend '0' when needed
hh = hh < 10 ? "0" + hh : hh;
mm = mm < 10 ? "0" + mm : mm;
ss = ss < 10 ? "0" + ss : ss;
//use it
var str = hh + ":" + mm + ":" + ss;
return str;
}
}
class StopwatchEventArgs {
private _ticks: number;
private _display: string;
get ticks() {
return this._ticks;
}
get display() {
return this._display;
}
constructor(ticks: number, display: string) {
this._ticks = ticks;
this._display = display;
}
}
declare var window: any;
declare var alert: any;
window.onload = function() {
var sw = new Stopwatch();
sw.onStart.subscribe((sender, args) => {
alert("Stopwatch started after " + args.display + ".");
});
sw.onPause.subscribe((sender, args) => {
alert("Paused after " + args.display + ".");
});
sw.start();
window.setTimeout(function() {
sw.pause();
}, 3000);
window.setTimeout(function() {
sw.start();
}, 4000);
};