From 906f43376bd849ccd967d1c144531202a566ae6b Mon Sep 17 00:00:00 2001 From: Josh de Leeuw Date: Fri, 8 Dec 2017 21:13:38 -0500 Subject: [PATCH] implements on_start #483 --- jspsych.js | 17 +++++++++--- tests/events.test.js | 62 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/jspsych.js b/jspsych.js index bea9e04a..a5daedb2 100755 --- a/jspsych.js +++ b/jspsych.js @@ -772,9 +772,6 @@ window.jsPsych = (function() { function doTrial(trial) { - // call experiment wide callback - opts.on_trial_start(trial); - current_trial = trial; // process all timeline variables for this trial @@ -786,6 +783,14 @@ window.jsPsych = (function() { // get default values for parameters setDefaultValues(trial); + // call experiment wide callback + opts.on_trial_start(trial); + + // call trial specific callback if it exists + if(typeof trial.on_start == 'function'){ + trial.on_start(trial); + } + // execute trial method jsPsych.plugins[trial.type].trial(DOM_target, trial); } @@ -950,6 +955,12 @@ jsPsych.plugins = (function() { default: {}, description: 'Data to add to this trial (key-value pairs)' }, + on_start: { + type: module.parameterType.FUNCTION, + pretty_name: 'On start', + default: function() { return; }, + description: 'Function to execute when trial begins' + }, on_finish: { type: module.parameterType.FUNCTION, pretty_name: 'On finish', diff --git a/tests/events.test.js b/tests/events.test.js index 76388ad0..8eacc4b5 100644 --- a/tests/events.test.js +++ b/tests/events.test.js @@ -64,6 +64,68 @@ describe('on_finish (trial)', function(){ }); }); +describe('on_start (trial)', function(){ + + test('should get trial data with function parameters evaluated', function(){ + require('../jspsych.js'); + require('../plugins/jspsych-html-keyboard-response.js'); + + return (new Promise(function(resolve, reject){ + + var d = null; + + var trial = { + type: 'html-keyboard-response', + stimulus: function(){ return 'hello'; }, + on_start: function(trial){ + d = trial.stimulus; + } + } + + jsPsych.init({ + timeline: [trial], + on_finish: function() { + resolve(d); + } + }); + + utils.pressKey(32); + + })).then(function(data) { expect(data).toBe('hello') }); + }); + + test('should get trial data with timeline variables evaluated', function(){ + require('../jspsych.js'); + require('../plugins/jspsych-html-keyboard-response.js'); + + return (new Promise(function(resolve, reject){ + + var d = null; + + var trial = { + timeline: [{ + type: 'html-keyboard-response', + stimulus: jsPsych.timelineVariable('stimulus'), + on_start: function(trial){ + d = trial.stimulus; + } + }], + timeline_variables: [{stimulus: 'hello'}] + } + + jsPsych.init({ + timeline: [trial], + on_finish: function() { + resolve(d); + } + }); + + utils.pressKey(32); + + })).then(function(data) { expect(data).toBe('hello') }); + }); +}) + describe('on_trial_finish (experiment level)', function(){ test('should get an object containing the trial data', function(){ require('../jspsych.js');