present next trial based on condition #776
-
Hi Josh, I would like to dynamically determine which trial will occur next based on the fulfillment of some condition. For instance, I want a survey trial to be presented next to the participant if the participant pressed the letter 'f'. I want the experiment to bypass the survey trial and instead progress to the next image trial if the participant pressed 'k'. Is there any way to pull out the key_press data from a previous trial (i.e. timeline object), evaluate the selection made previously in this timeline object, and then have the new timeline object execute either option A (presenting the survey trial) or option B (moving on to the next image trial as planned)? I have tried the if(data.key_press == 75){//do something} function I saw on the jsPsych website, however, it doesn't appear to work if placed within a new timeline object. Any help would be appreciated! Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Sounds like the survey trial should be wrapped in a conditional timeline. The example on the docs page is very similar to what you need to do. Copying it here for ease of reference: 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.'
}
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(){
// get the data from the previous trial,
// and check which key was pressed
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: [pre_if_trial, if_node, after_if_trial],
on_finish: function(){jsPsych.data.displayData(); }
}); |
Beta Was this translation helpful? Give feedback.
Sounds like the survey trial should be wrapped in a conditional timeline. The example on the docs page is very similar to what you need to do. Copying it here for ease of reference: