-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.js
105 lines (89 loc) · 1.88 KB
/
timer.js
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
jsx.dom.Timer = function(f, oProperties) {
if (f){this.form = f;}
this.message = "";
if (typeof Object.keys == "function")
{
var keys = Object.keys(oProperties);
for (var i = 0, len = keys.length; i < len; ++i)
{
var p = keys[i];
this[p] = oProperties[p];
}
}
else
{
for (p in oProperties)
{
this[p] = oProperties[p];
}
}
};
jsx.dom.Timer.prototype = {
constructor: jsx.dom.Timer,
start: function(o)
{
if (o){this.form = o.form;}
// clear the previous timeout
this.stop();
if (jsx.object.isMethod(window, "setTimeout"))
{
var me = this;
this.timeout = window.setTimeout(
function() { me.tick(1000); me = null; },
1000);
}
},
tick: function(interval)
{
var
es = this.form.elements,
hours = +es["hours"].value,
mins = +es["minutes"].value,
secs = +es["seconds"].value;
if (--secs < 0)
{
secs = 59;
mins--;
}
if (mins < 0)
{
mins = 59;
hours--;
}
if (hours < 0)
{
this.ring();
}
else
{
es["hours"].value = hours;
es["minutes"].value = leadingZero(mins, 2);
es["seconds"].value = leadingZero(secs, 2);
var me = this;
this.timeout = window.setTimeout(
function() { me.tick(interval); me = null; },
interval);
}
},
ring: function()
{
this.stop();
playSound('/media/audio/sound/display.wav');
window.alert(this.message || "RING!");
},
stop: function(o)
{
if (!this.form && o){this.form = o.form;}
if (typeof this.timeout != "undefined"
&& jsx.object.isMethod(window, "clearTimeout"))
{
window.clearTimeout(this.timeout);
delete this.timeout;
}
},
reset: function(o)
{
this.stop();
if (this.form){this.form.reset();}
}
};