forked from jspsych/jsPsych
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconditional-and-loop-functions.html
63 lines (55 loc) · 1.56 KB
/
conditional-and-loop-functions.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
<!DOCTYPE html>
<html>
<head>
<script src="../jspsych.js"></script>
<script src="../plugins/jspsych-html-keyboard-response.js"></script>
<link rel="stylesheet" href="../css/jspsych.css"></link>
</head>
<body></body>
<script>
var trial = {
type: 'html-keyboard-response',
stimulus: 'Hello. This is in a loop. Press R to repeat this trial, or C to continue.',
choices: ['r','c']
}
var loop_node = {
timeline: [trial],
loop_function: function(data){
if(jsPsych.pluginAPI.convertKeyCharacterToKeyCode('r') == data.values()[0].key_press){
return true;
} else {
return false;
}
}
}
var pre_if_trial = {
type: 'html-keyboard-response',
stimulus: 'The next trial is in a conditional statement. Press S to skip it, or V to view it.',
choices: ['s','v']
}
var if_trial = {
type: 'html-keyboard-response',
stimulus: 'You chose to view the trial. Press any key to continue.'
}
var if_node = {
timeline: [if_trial],
conditional_function: function(){
var data = jsPsych.data.get().last(1).values()[0];
if(data.key_press == jsPsych.pluginAPI.convertKeyCharacterToKeyCode('s')){
return false;
} else {
return true;
}
}
}
var after_if_trial = {
type: 'html-keyboard-response',
stimulus: 'This is the trial after the conditional.'
}
jsPsych.init({
timeline: [loop_node, pre_if_trial, if_node, after_if_trial],
on_finish: function(){ jsPsych.data.displayData(); },
default_iti: 200
});
</script>
</html>