diff --git a/docs/markdown_docs/core_library/jspsych-data.md b/docs/markdown_docs/core_library/jspsych-data.md index 0374c99b..c47f84d6 100644 --- a/docs/markdown_docs/core_library/jspsych-data.md +++ b/docs/markdown_docs/core_library/jspsych-data.md @@ -2,6 +2,46 @@ The jsPsych.data module contains functions for interacting with the data generated by jsPsych plugins. +--- +## jsPsych.data.addDataToLastTrial + +``` +jsPsych.data.addDataToLastTrial(data) +``` + +### Parameters + +Parameter | Type | Description +----------|------|------------ +data | object | Object of key: value pairs to add to the data from the last trial. + +### Return value + +Returns nothing. + +### Description + +This method appends data to the data recorded by the last trial. It's particularly useful when combined with the `on_finish` event handler for a trial, as shown in the example below. + + +### Examples + +#### Evaluate a response and add to data +```javascript +var block = { + type: 'single-stim', + stimuli: ['img/happy_face_1.jpg', 'img/sad_face_1.jpg'], + choices: [89,78], // Y or N + prompt: '

Have you seen this face before? Y or N.

', + on_finish: function(trial_data){ + // let's imagine that the correct answer is NO for both trials + var correct = (trial_data.key_press == 78); + jsPsych.data.addDataToLastTrial({correct: correct}); + } +} +``` + + --- ## jsPsych.data.addProperties diff --git a/docs/markdown_docs/core_library/overview.md b/docs/markdown_docs/core_library/overview.md index cf6396f3..085759d1 100644 --- a/docs/markdown_docs/core_library/overview.md +++ b/docs/markdown_docs/core_library/overview.md @@ -21,6 +21,7 @@ Every jsPsych experiment utilizes the core library (contained in the `jspsych.js ### [Data module](jspsych-data.md) +* [jsPsych.data.addDataToLastTrial](jspsych-data.md#jspsychdataadddatatolasttrial) * [jsPsych.data.addProperties](jspsych-data.md#jspsychdataaddproperties) * [jsPsych.data.dataAsCSV](jspsych-data.md#jspsychdatadataascsv) * [jsPsych.data.displayData](jspsych-data.md#jspsychdatadisplaydata) diff --git a/jspsych.js b/jspsych.js index 97b24218..106fb945 100755 --- a/jspsych.js +++ b/jspsych.js @@ -645,7 +645,13 @@ // now add to list so that it gets appended to all future data dataProperties = $.extend({}, dataProperties, properties); + }; + module.addDataToLastTrial = function(data){ + if(allData.length == 0){ + throw new Error("Cannot add data to last trial - no data recorded so far"); + } + allData[allData.length-1] = $.extend({},allData[allData.length-1],data); } module.dataAsCSV = function() { diff --git a/tests&examples/data-add-to-last-trial.html b/tests&examples/data-add-to-last-trial.html new file mode 100644 index 00000000..00b88b0a --- /dev/null +++ b/tests&examples/data-add-to-last-trial.html @@ -0,0 +1,40 @@ + + + + + + + + + + +
+ + +