-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions5.js
127 lines (107 loc) · 3.52 KB
/
functions5.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
$(document).ready(function() {
$("#start5").click(
function() {
$(".dis").prop('disabled', true);
var flashes = [];
var milis = [];
const s_color = $("#s-color").val();
const ISI = $("#duration_of_stimulus").val();
const d_s = 100;
const time = d_s + ISI;
const n_t = $("#number_of_trials").val();
number_of_trials = n_t;
var all_chars = [1,2,3,4,5,6,7,8];
new_chars = shuffle(all_chars);
number_of_trials--;
for(a=0; a<number_of_trials; a++) {
temp_chars = shuffle(all_chars);
new_chars = new_chars.concat(temp_chars);
if(a == number_of_trials-1){
document.getElementById("data").innerHTML = new_chars;
}
}
c=new_chars.length;
i=0;
var d = new Date();
var h = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds();
var n = d.getMilliseconds();
var startTime = h + ":" + m + ":" + s + " -- " + "you choosed the fifth protocol";;
var fix_s = s+5;
var firstStimulus = m + ":" + fix_s;
document.getElementById("time").innerHTML = startTime;
document.getElementById("f_s").innerHTML = firstStimulus;
setTimeout(flash,5000);
// 2 second pause before stimulus presentation starts
var flash_time = d_s;
function flash() {
if(i<c) {
var flash_index = new_chars[i];
requestAnimationFrame(() => {
light_unlit(flash_index,1); // highlight element
var d = new Date();
var m = d.getMinutes();
var s = d.getSeconds();
var n = d.getMilliseconds();
//var timer = m + ":" + s;
//document.getElementById("timer").innerHTML = timer;
var mili_s = m*60*1000+1000*s+n;
milis.push(mili_s);
new_time = (m + "," + s + "," + n);
flashes.push(new_time)
})
setTimeout(
function() {
light_unlit(flash_index,0); // revert element to default colour after flash
setTimeout(flash,ISI);
}
,flash_time);
}
i++;
if(i == c+1 && flashes){
for(i=0;i<milis.length-1;i++){
milis[i] = -milis[i] + milis[i+1] - (time) + 99900
}
var total = 0;
for(j = 0; j < milis.length-1; j++) {
total += milis[j];
}
console.log(milis,total)
var avg = total / (milis.length-1);
flashes.push("Mean Error = " + avg)
document.getElementById("data_time").innerHTML = flashes.join('\r\n');
$(".dis").prop('disabled', false);
}
}
// recursive function to keep calling setTimeout until all characters have flashed
function light_unlit(char_index) {
switch(char_index) {
case 1: $(".a1").toggleClass( s_color ); break;
case 2: $(".a2").toggleClass( s_color ); break;
case 3: $(".a3").toggleClass( s_color ); break;
case 4: $(".a4").toggleClass( s_color ); break;
case 5: $(".a5").toggleClass( s_color ); break;
case 6: $(".a6").toggleClass( s_color ); break;
case 7: $(".a7").toggleClass( s_color ); break;
case 8: $(".a8").toggleClass( s_color ); break;
default:
}
}
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
}
);
});