implements on_start #483

This commit is contained in:
Josh de Leeuw 2017-12-08 21:13:38 -05:00
parent 35bff7baa3
commit 906f43376b
2 changed files with 76 additions and 3 deletions

View File

@ -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',

View File

@ -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');