-
Notifications
You must be signed in to change notification settings - Fork 4
/
countdown.js
92 lines (82 loc) · 1.94 KB
/
countdown.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
function Countdown()
{
this.start_time = "25:00";
this.end_time = "0:00";
this.target_id = "countdown_timer";
this.paused = true;
this.name = 'timer';
this.eventHandlers = {};
this.complete = true;
}
Countdown.prototype.target = function()
{
return $("#" + this.target_id);
}
Countdown.prototype.update_target = function()
{
minutes = this.minutes;
seconds = this.seconds;
if(seconds < 10) seconds = "0" + seconds;
$("#" + this.target_id).val(minutes + ":" + seconds);
}
Countdown.prototype.pause = function()
{
if(!this.paused){
this.paused = true;
this.fire('pause');
}
}
Countdown.prototype.resume = function()
{
this.paused = false;
this.fire('play');
}
Countdown.prototype.init = function()
{
this.reset();
setInterval(this.name + '.tick()', 1000);
}
Countdown.prototype.tick = function()
{
if(!this.paused && !(this.seconds <= 0 && this.minutes <=0)){
this.complete = false;
this.seconds = this.seconds - 1;
if(this.seconds <= 0 && this.minutes > 0){
this.minutes--;
this.seconds = 59;
}
}else{
if(this.seconds <= 0 && this.minutes <=0 && !this.complete) {
this.fire('complete');
this.complete = true;
}
}
this.update_target();
}
Countdown.prototype.start = function()
{
this.reset();
this.paused = false;
}
Countdown.prototype.reset = function(time)
{
if(time == undefined) time = this.start_time;
time_ary = time.split(":");
this.minutes = parseInt(time_ary[0]);
this.seconds = parseInt(time_ary[1]);
this.update_target();
if(!this.paused)
{
this.paused = true;
this.fire('stop');
}
this.complete = false;
}
Countdown.prototype.registerHandler = function(event, data, handler)
{
this.target().bind(event, data, handler);
}
Countdown.prototype.fire = function(event)
{
this.target().trigger(event)
}