-
Hi, I would like to add a conditional trial to the end of the timeline which only runs after every 8th timeline variable is selected, as proposed in this old Google Groups thread. I just can't seem to figure out how to accomplish this (still very new to JavaScript/jsPsych). My timeline now looks like this: timeline.push({
timeline: [
{
type: 'html-keyboard-response',
stimulus: "<div style='font-size:32px;'>+</div>",
choices: jsPsych.NO_KEYS,
trial_duration: 1000
},
{
type: 'audio-keyboard-response',
stimulus: jsPsych.timelineVariable('audio'),
choices: jsPsych.NO_KEYS,
trial_ends_after_audio: true
},
{
type: 'html-button-response',
stimulus: "<p>Which question did the speaker answer?<br></p>",
choices: [jsPsych.timelineVariable('questionBF'),
jsPsych.timelineVariable('questionSF'),
jsPsych.timelineVariable('questionVF'),
jsPsych.timelineVariable('questionOF')],
button_html: '<button class="jspsych-btn-block">%choice%</button>',
margin_vertical: '12px',
data: jsPsych.timelineVariable('data'),
on_finish: function(data){
if(data.button_pressed == data.correct_button){
data.correct = true;
} else {
data.correct = false;
}
}
}
],
// timeline_variables: [ ],
sample: {
type: 'custom',
fn: function(){
var groups = [[0,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]];
var all_items = [];
for(var i=0; i<groups.length; i++){
for(var j=0; j<groups[i].length; j++){
all_items.push({group: i, trial_num: groups[i][j]});
}
}
var order = jsPsych.randomization.shuffleNoRepeats(all_items, function(a,b){
return a.group == b.group;
});
return order.map(function(x){ return x.trial_num });
}
}
}); I want the conditional trial to be added to the end of the timeline so it will be displayed after participants give a button response (the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
One solution is to somehow get the conditional function to count how many iterations through the timeline have happened so far, and if it is divisible by 8 then we should show the conditional trial. To do this, I needed to add some kind of marker to the data that would exist once for each loop. You might already have such a marker in In the conditional function, I used timeline.push({
timeline: [
{
type: 'html-keyboard-response',
stimulus: "<div style='font-size:32px;'>+</div>",
choices: jsPsych.NO_KEYS,
trial_duration: 1000,
data: {phase: 'fixation'}
},
{
type: 'audio-keyboard-response',
stimulus: jsPsych.timelineVariable('audio'),
choices: jsPsych.NO_KEYS,
trial_ends_after_audio: true
},
{
type: 'html-button-response',
stimulus: "<p>Which question did the speaker answer?<br></p>",
choices: [jsPsych.timelineVariable('questionBF'),
jsPsych.timelineVariable('questionSF'),
jsPsych.timelineVariable('questionVF'),
jsPsych.timelineVariable('questionOF')],
button_html: '<button class="jspsych-btn-block">%choice%</button>',
margin_vertical: '12px',
data: jsPsych.timelineVariable('data'),
on_finish: function(data){
if(data.button_pressed == data.correct_button){
data.correct = true;
} else {
data.correct = false;
}
}
},
{
timeline: [...]
conditional_function: function(){
return jsPsych.data.get().filter({phase: 'fixation'}).count() % 8 == 0;
}
}
],
// timeline_variables: [ ],
sample: {
type: 'custom',
fn: function(){
var groups = [[0,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]];
var all_items = [];
for(var i=0; i<groups.length; i++){
for(var j=0; j<groups[i].length; j++){
all_items.push({group: i, trial_num: groups[i][j]});
}
}
var order = jsPsych.randomization.shuffleNoRepeats(all_items, function(a,b){
return a.group == b.group;
});
return order.map(function(x){ return x.trial_num });
}
}
}); |
Beta Was this translation helpful? Give feedback.
One solution is to somehow get the conditional function to count how many iterations through the timeline have happened so far, and if it is divisible by 8 then we should show the conditional trial.
To do this, I needed to add some kind of marker to the data that would exist once for each loop. You might already have such a marker in
jsPsych.timelineVariable('data')
. Since I couldn't see what was in those variables I added one to the fixation trial:data: {phase: 'fixation'}
.In the conditional function, I used
jsPsych.data.get().filter({phase: 'fixation'}).count()
to count the number of fixation trials so far. I just return this value%
8, which should betrue
after every 8 trials.timeline