-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.js
102 lines (78 loc) · 2.41 KB
/
logic.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
$( document ).ready(function() {
var count = 0;
var goal = 0;
var base_time = 0;
var time_tracker = 0;
var timer;
var time_left;
//Increments hydration count
function closer(){
count = count + 1;
}
//Resets hydration trackers
function reset(){
count = 0;
goal = 0;
base_time = 0;
time_tracker = 0;
}
//Returns a string representation of the time left in this round
function remaining_time(){
var duration = base_time - ( (new Date()).getTime() - time_tracker );
var seconds = parseInt((duration / 1000) % 60);
var minutes = parseInt((duration / (1000 * 60)) % 60);
var hours = parseInt((duration / (1000 * 60 * 60)) % 24);
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
return hours + ":" + minutes + ":" + seconds;
}
//Checks for hydration success and cleans up from now-previous round
function finish(){
$("#param-wrapper").css('display', 'block');
$("#watr-wrapper").css('display', 'none');
if ( count >= goal ) {
$("#success").css('display', 'block');
}
else {
$("#failure").css('display', 'block');
}
reset();
}
//Hides achievment indicators on next round initiation
$("input").click( function(){
$("#success").css('display', 'none');
$("#failure").css('display', 'none');
});
//Hides our parameter entry and starts the next round
$("#start-timing").click( function(){
$("#param-wrapper").css('display', 'none');
goal = $("#glasses").val();
$("#glasses").val('');
base_time = ($("#duration").val()) * 3600 * 1000;
$("#duration").val('');
$("#count-print").text( count );
$("#goal-print").text( goal );
$("#watr-wrapper").css('display', 'block');
time_tracker = (new Date()).getTime();
timer = setTimeout( finish, base_time );
});
$("#reset").click( function(){
clearTimeout( timer );
finish();
});
$("#tracker").click( function(){
});
//Makes sure our achievment indicators are hidden at the beginning
$("#success").css('display', 'none');
$("#failure").css('display', 'none');
//Updates the hydration timer
setInterval(function() {
$("#time").text( remaining_time() );
}, 1000);
$("#tracker-wrap").click( function(){
closer();
$("#count-print").text( count );
$("#goal-print").text( goal );
});
});