-
Notifications
You must be signed in to change notification settings - Fork 1
/
Week9-Dynamic Trials Pt3-bonus.html
executable file
·131 lines (110 loc) · 3.72 KB
/
Week9-Dynamic Trials Pt3-bonus.html
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
<!DOCTYPE html>
<html>
<head>
<title>Week 9: Dynamic Trials Pt3</title>
<!-- Load JS psych -->
<script src="jspsych-7.2/jspsych.js"></script>
<!-- Load all the plugins we will be using today -->
<script src="jspsych-7.2/plugin-html-keyboard-response.js"></script>
<script src="jspsych-7.2/plugin-html-button-response.js"></script>
<!-- Load jspsych's default CSS -->
<link href="jspsych-7.2/jspsych.css" rel="stylesheet" type="text/css"></link>
</head>
<body>
</body>
<script>
// Overview of jspsych plugins: https://www.jspsych.org/plugins/overview/
// Overview of jspsych plugins: https://www.jspsych.org/plugins/overview/
// Initialize jsPsych
var jsPsych = initJsPsych({
on_finish: function() {
jsPsych.data.displayData(); // let's see the data in json format
jsPsych.data.get().localSave('csv','mydata.csv'); // save the data in csv format in a file called 'mydata.csv' (format, filename)
}
});
// Define an empty timeline array
var timeline = [];
/*/ Define the trial variables /*/
var welcome = {
type: jsPsychHtmlKeyboardResponse,
stimulus: `Welcome to the study!<p>
Please choose the correct category (dog or cat) for each animal.<p>
Please try to respond as quickly as possible without sacrificing accuracy.<p>
<p>Press ENTER to begin.`,
choices: ['enter']
};
timeline.push(welcome)
// Initialize counter variable
var count = 0
var timeLimit = 5000
// Array of stimuli
var animals = ["img/cat1.jpg","img/cat2.jpg","img/dog1.jpg","img/dog2.jpg"]
var animals = jsPsych.randomization.shuffle(animals)
// Show an image of a cat or a dog, with response buttons.
var catdog = {
type: jsPsychHtmlButtonResponse,
stimulus: function() {
current_animal = animals[count]
// count++
return `<img src = ${current_animal} width=200px></p>`
},
choices: ['Cat','Dog'],
trial_duration: timeLimit
};
// Change the feedback stimulus parameter so it says "Correct" or "Wrong" depending on whether the answer was correct or not.
var feedback3 = {
type: jsPsychHtmlKeyboardResponse,
stimulus: function() {
// the shown animal
shown = "dog"
if (current_animal.includes("cat")) {
shown = "cat"
}
// the chosen animal
button_pressed = jsPsych.data.getLastTrialData().values()[0].response
if (button_pressed==0) {
chosen = "cat"
} else if (button_pressed==1) {
chosen = "dog"
} else {
chosen = "none"
}
// remember to use == for logical comparison.
if (chosen == shown) {
correct = 1
return "Correct!"
} else {
correct = 0
return "Wrong!"
}
},
choices: "NO_KEYS",
trial_duration: 1000,
on_finish: function(data) {
data.shown = shown;
data.chosen = chosen;
data.correct = correct
}
};
// Bonus: read the documentation for looping timelines here: https://www.jspsych.org/overview/timeline/#looping-timelines
// Can we use this to repeat an catdog trial if the subject gets that trial wrong?
// This is tricky because we only want to increment our counter when correct==1.
var catdog_loop = {
timeline: [catdog,feedback3],
loop_function: function(data){
if(correct == 1){ // break loop if they got it right
count++ // moved from line 45
return false;
} else {
return true;
}
}
}
// push catdog loops into timeline
for (i=0; i < animals.length; i++) {
timeline.push(catdog_loop)
}
/*/ Run the timeline /*/
jsPsych.run(timeline);
</script>
</html>