jsPsych/examples/conditional-and-loop-functions.html
2017-07-17 10:19:41 -04:00

64 lines
1.6 KiB
HTML

<!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>