jspsych / jsPsych

Create behavioral experiments in a browser using JavaScript
http://www.jspsych.org
MIT License
1.03k stars 668 forks source link

timing_post_trial and data parameters evaluate twice when they are functions #158

Closed jodeleeuw closed 8 years ago

jodeleeuw commented 9 years ago

From the google group:

If you define a function for the post trial interval like so (the reason for updating the variable "gap" will become clear momentarily):

var post_trial_gap = function() {
  gap = Math.floor( Math.random() * 1500 ) + 750
  return gap;
}

and pass it to "timing_post_trial" the function will be called TWICE for each trial. The first call happens at the trial's onset and the second call happens after a response (presumably when "timing_post_trial" must retrieve a value).

While redundant calls aren't that bad, an issue arises if I want to record what the post-trial timing was for a particular trial by passing a function like:

var record_gap = function() {
     jsPsych.data.addDataToLastTrial({gap:gap})
}

The function will append the return from the FIRST call of post_trial_gap to "data" while the second call of post_trial_gap will be passed to "timing_post_trial".

So the gap variable would look something like this on a particular trial: CALL post_trial_gap; gap = 537 CALL record_gap; data for trial updated with {gap: 537} CALL post_trial_gap; gap = 700 timing_post_trial = 700

The end result is that the actual trial times are not reflected in the data structure.

One way around this is to update the gap variable with the "record_gap" function and change the post_trial_gap a simple get function:

var record_gap = function() {
   gap = Math.floor( Math.random() * 1500 ) + 750
     jsPsych.data.addDataToLastTrial({gap:gap})
}
var post_trial_gap = function() {
  return gap;
}
jodeleeuw commented 9 years ago

The reason this happens is because timing_post_trial and data both get sent into the plugin itself and evaluated, but then also get evaluated when the core library actually uses the values. The plugin evaluated versions are not saved anywhere external to the plugin trial function, so there's no lookup available of the evaluated version.