diff --git a/jspsych.js b/jspsych.js index 188e8a0c..1cf2a093 100755 --- a/jspsych.js +++ b/jspsych.js @@ -489,8 +489,8 @@ window.jsPsych = (function() { // check for on_timeline_start and conditonal function on nodes with timelines if (typeof timeline_parameters != 'undefined') { // only run the conditional function if this is the first repetition of the timeline when - // repetitions > 1 - if (typeof timeline_parameters.conditional_function !== 'undefined' && progress.current_repetition==0) { + // repetitions > 1, and only when on the first variable set + if (typeof timeline_parameters.conditional_function !== 'undefined' && progress.current_repetition==0 && progress.current_variable_set == 0) { var conditional_result = timeline_parameters.conditional_function(); // if the conditional_function() returns false, then the timeline // doesn't run and is marked as complete. diff --git a/tests/jsPsych/timelines.test.js b/tests/jsPsych/timelines.test.js index ec5a8eca..d96a2a48 100644 --- a/tests/jsPsych/timelines.test.js +++ b/tests/jsPsych/timelines.test.js @@ -301,6 +301,41 @@ describe('conditional function', function(){ expect(conditional_count).toBe(1); }) + test('executes only once when timeline variables are used', function(){ + var conditional_count = 0; + + var trial = { + timeline: [{ + type: 'html-keyboard-response', + stimulus: 'foo' + }], + timeline_variables: [ + {a:1}, + {a:2} + ], + conditional_function: function(){ + conditional_count++; + return true; + } + } + + jsPsych.init({ + timeline: [trial] + }); + + expect(conditional_count).toBe(1); + + // first trial + utils.pressKey(32); + + expect(conditional_count).toBe(1); + + // second trial + utils.pressKey(32); + + expect(conditional_count).toBe(1); + }) + test('timeline variables from nested timelines are available', function(){ var trial = { type: 'html-keyboard-response',