-
Hello everyone! I am coding an experiment in which a participant presses the spacebar as many times as she/he can for 3 trials in a row. Afterwards, I would like to compute the mean of the spacebar presses across the 3 trials and use it as a timelineVariable for the next trials of the experiment. To average across these three trials I use a function, which I call when I declare the timelineVariables. However, the timelineVariable results as 'undefined' (i.e. the trial named below as titration_procedure show the word undefined when the timelineVariable "effort_B" is called, instead of the average of spacebar presses). Can somebody help me? Here the code: var n_compute_max_effort = 3
var compute_max_effort = {
type: 'keypress-counter',
key2press: ['space'],
num_presses_ends_trial: false,
trial_duration: 2000,
post_trial_gap: 2000
}
var repeat_max_effort = {
timeline: [compute_max_effort],
repetitions: n_compute_max_effort
}
function generateMaxEff() {
try {
var keypress_1 = jsPsych.data.getDataByTimelineNode("0.0-1.0-0.0").values()[0].count
var keypress_2 = jsPsych.data.getDataByTimelineNode("0.0-1.0-0.1").values()[0].count
var keypress_3 = jsPsych.data.getDataByTimelineNode("0.0-1.0-0.2").values()[0].count
var tot_presses = keypress_1 + keypress_2 + keypress_3
var avg_presses = tot_presses / n_compute_max_effort
console.log(avg_presses)
return avg_presses
} catch (err){
console.log('Max effort not yet estimated')
}
}
var titration_procedure = {
type: 'html-button-response',
choices: function (){
return ['<p style="font-size: 48px;">Offerta A: <br><br><b>'+jsPsych.timelineVariable('effort_A', true)+'</b> pressioni <br>per <br><b>'+jsPsych.timelineVariable('offer_A', true)+'</b> euro</p>',
'<p style="font-size: 48px;">Offerta B: <br><br><b>'+jsPsych.timelineVariable('effort_B', true)+'</b> pressioni <br>per <br><b>'+jsPsych.timelineVariable('offer_B', true)+'</b> euro</p>'
]
},
prompt: '<p style="font-size: 32px;">Quale offerta preferisci?</p>',
data: {
do_the_effort: jsPsych.timelineVariable('is_effort_required')
}
}
var spacebar_pression = {
type: 'keypress-counter',
key2press: ['space'],
num_presses_ends_trial: true,
num_presses: function() {
var sc = jsPsych.data.getLastTrialData().values()[0].effort_required
return sc
},
trial_duration: 2000
}
var effort = {
timeline: [spacebar_pression],
conditional_function: function(){
var is_eff_req = jsPsych.data.getLastTrialData().values()[0].do_the_effort;
if(is_eff_req){
return true;
} else {
return false;
}
}
}
var main_procedure = {
timeline: [titration_procedure, effort],
timeline_variables: [
{effort_A: 2, effort_B: generateMaxEff(), offer_A: 33, offer_B: 72, is_effort_required: false},
],
// randomize_order: true
}
jsPsych.init({
timeline: [fullscreen_trial, repeat_max_effort, main_procedure, fullscreen_trial_exit],
on_finish: function(){jsPsych.data.displayData();}
})
` |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @t-09, The problem here is that this chunk of code runs before the experiment starts: var main_procedure = {
timeline: [titration_procedure, effort],
timeline_variables: [
{effort_A: 2, effort_B: generateMaxEff(), offer_A: 33, offer_B: 72, is_effort_required: false},
],
// randomize_order: true
} Which means that Since you are computing this value dynamically I'm not sure if you need to use a timeline variable. Could you just do something like this instead? var titration_procedure = {
type: 'html-button-response',
choices: function (){
var effortB = generateMaxEff();
return ['<p style="font-size: 48px;">Offerta A: <br><br><b>'+jsPsych.timelineVariable('effort_A', true)+'</b> pressioni <br>per <br><b>'+jsPsych.timelineVariable('offer_A', true)+'</b> euro</p>',
'<p style="font-size: 48px;">Offerta B: <br><br><b>'+effortB+'</b> pressioni <br>per <br><b>'+jsPsych.timelineVariable('offer_B', true)+'</b> euro</p>'
]
},
prompt: '<p style="font-size: 32px;">Quale offerta preferisci?</p>',
data: {
do_the_effort: jsPsych.timelineVariable('is_effort_required')
}
} |
Beta Was this translation helpful? Give feedback.
Hi @t-09,
The problem here is that this chunk of code runs before the experiment starts:
Which means that
generateMaxEff()
is called before any data exist.Since you are computing this value dynamically I'm not sure if you need to use a timeline variable. Could you just do something like this instead?