-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
175 lines (133 loc) · 4.34 KB
/
test.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//=================== Timer class ==============================
function CountDownTimer(duration, granularity) {
this.duration = duration;
this.granularity = granularity || 1000;
this.tickFtns = [];
this.running = false;
}
CountDownTimer.prototype.start = function() {
if (this.running) {
return;
}
this.running = true;
if (this.hasOwnProperty("alarmTime")) {
this.toggleAlarmOn = true;
}
var start = Date.now(),
that = this,
diff, obj,
timeoutID;
(function timer() {
diff = that.duration - (((Date.now() - start) / 1000) | 0);
if (diff > 0) {
timeoutID = setTimeout(timer, that.granularity);
} else {
diff = 0;
that.running = false;
}
if ((diff < that.alarmTime) && that.toggleAlarmOn) {
that.toggleAlarmOn = false; // make sure that the alarm is called once;
that.alarmFtn.call(this);
}
obj = CountDownTimer.parse(diff);
that.tickFtns.forEach(function(ftn) {
ftn.call(this, obj.minutes, obj.seconds);
}, that);
return timeoutID;
}());
var resetTimer = function() {
if (that.running) {
clearTimeout(timeoutID);
that.running = false;
that.duration = 0;
}
}
return resetTimer;
};
CountDownTimer.prototype.onTick = function(ftn) {
if (typeof ftn === 'function') {
this.tickFtns.push(ftn);
}
return this;
};
CountDownTimer.prototype.onAlarm = function(ftn) {
if (typeof ftn === 'function') {
this.alarmFtn = ftn;
}
return this;
};
CountDownTimer.prototype.expired = function() {
return !this.running;
};
CountDownTimer.prototype.setTime = function(secs) {
this.duration = secs;
}
CountDownTimer.prototype.setAlarmTime = function(alarmTime) {
this.alarmTime = alarmTime;
}
CountDownTimer.prototype.reset = function(timeoutID) {}
CountDownTimer.parse = function(seconds) {
return {
'minutes': (seconds / 60) | 0,
'seconds': (seconds % 60) | 0
};
};
// ===================================================================
var globalIndex = 0;
var timer;
var reset;
var previousTime = 0;
window.onload = function() {
console.log("========== Starting experiment ===========");
// console.log("Start,End,Label")
$("#resultlabel").append("Start,End,Label\n");
updateSelection(0);
display = document.querySelector('#timer');
globalIndex = 0;
$('#ActionGuide').text(experiment[globalIndex].action);
timer = new CountDownTimer(experiment[globalIndex].secs);
timer.setAlarmTime(1);
timer.onAlarm(playsound);
reset = timer.onTick(format).start();
previousTime = Date.now();
};
function updateSelection(newIndex) {
globalIndex = newIndex;
$('#ActionGuide').text(experiment[globalIndex].action);
var currentTime = Date.now();
if (newIndex > 0) {
$("#resultlabel").append(previousTime + "," + currentTime + "," + experiment[globalIndex - 1].action+"\n");
}
previousTime = currentTime;
// var currentTime = Date.now();
// var nextTime = currentTime + 1000*experiment[globalIndex].secs;
// var output = currentTime + "," + nextTime + "," + experiment[globalIndex].action+"\n";
// console.log(output);
// $("#resultlabel").append(output);
}
function format(minutes, seconds) {
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
$('#timer').text(minutes + ':' + seconds);
if (this.expired())
restart();
}
function restart() {
if (globalIndex < experiment.length - 1) {
updateSelection(globalIndex + 1);
timer.setTime(experiment[globalIndex].secs);
reset = timer.start();
}
$('#timer').css('color', 'black')
}
function playsound() {
var snd = new Audio('elevator.mp3');
snd.play();
if ((globalIndex < experiment.length - 1) && (globalIndex > 0)) {
$('#timer').css('color', 'purple')
// $('#ActionGuide').text('Prepare for the next action: ' + experiment[globalIndex + 1].action)
} else if (globalIndex == experiment.length - 1){
$("#resultlabel").append(previousTime + "," + Date.now() + "," + experiment[experiment.length - 1].action+"\n");
$('#ActionGuide').text('Done!')
}
}