This commit is contained in:
Josh de Leeuw 2015-06-26 15:30:26 -04:00
parent a14685f48c
commit 3ef2f16f22
4 changed files with 87 additions and 0 deletions

View File

@ -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: '<p class="center-content">Have you seen this face before? Y or N.</p>',
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

View File

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

View File

@ -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() {

View File

@ -0,0 +1,40 @@
<!doctype html>
<html>
<head>
<script src="js/jquery.min.js"></script>
<script src="../jspsych.js"></script>
<script src="../plugins/jspsych-single-stim.js"></script>
<link rel="stylesheet" href="../css/jspsych.css"></link>
<style>
img { width: 300px; }
</style>
</head>
<body>
<div id="jspsych-target"></div>
</body>
<script>
var block_1 = {
type: 'single-stim',
stimuli: ['img/happy_face_1.jpg', 'img/sad_face_1.jpg'],
choices: [89,78], // Y or N
prompt: '<p class="center-content">Have you seen this face before? Y or N.</p>',
on_finish: function(){
jsPsych.data.addDataToLastTrial({correct: true});
}
}
function start(){
jsPsych.init({
display_element: $('#jspsych-target'),
experiment_structure: [block_1],
on_finish: function(){
jsPsych.data.displayData();
}
});
}
jsPsych.preloadImages(['img/happy_face_1.jpg','img/sad_face_1.jpg'], start);
</script>
</html>