fix docs formatting issues, edit code blocks for consistency

This commit is contained in:
Becky Gilbert 2021-03-24 15:07:28 -07:00
parent bf9dec250b
commit 163f3479b0
6 changed files with 182 additions and 193 deletions

View File

@ -2,7 +2,8 @@
--- ---
## jsPsych.addNodeToEndOfTimeline ## jsPsych.addNodeToEndOfTimeline
```
```javascript
jsPsych.addNodeToEndOfTimeline(node_parameters) jsPsych.addNodeToEndOfTimeline(node_parameters)
``` ```
@ -20,9 +21,7 @@ None.
Adds the timeline to the end of the experiment. Adds the timeline to the end of the experiment.
### Examples ### Example
#### Without callback
```javascript ```javascript
var trial = { var trial = {
@ -37,28 +36,10 @@ var new_timeline = {
jsPsych.addNodeToEndOfTimeline(new_timeline) jsPsych.addNodeToEndOfTimeline(new_timeline)
``` ```
### With callback
```javascript
var first = {
type: 'html-keyboard-response',
stimulus: 'first trial; new trial added when on_finish is called',
on_finish: function(){
jsPsych.pauseExperiment();
jsPsych.addNodeToEndOfTimeline({
timeline: [{
type: 'image-keyboard-response',
stimulus: 'img/happy_face_4.jpg'
}]
}, jsPsych.resumeExperiment)
}
}
```
--- ---
## jsPsych.allTimelineVariables ## jsPsych.allTimelineVariables
``` ```javascript
jsPsych.allTimelineVariables() jsPsych.allTimelineVariables()
``` ```
@ -91,7 +72,7 @@ var trial = {
--- ---
## jsPsych.currentTimelineNodeID ## jsPsych.currentTimelineNodeID
``` ```javascript
jsPsych.currentTimelineNodeID() jsPsych.currentTimelineNodeID()
``` ```
@ -131,14 +112,13 @@ The rules about iterations apply throughout the hierarchical ID:
```javascript ```javascript
var id = jsPsych.currentTimelineNodeID(); var id = jsPsych.currentTimelineNodeID();
console.log('The current TimelineNode ID is '+id); console.log('The current TimelineNode ID is '+id);
``` ```
--- ---
## jsPsych.currentTrial ## jsPsych.currentTrial
``` ```javascript
jsPsych.currentTrial() jsPsych.currentTrial()
``` ```
@ -157,16 +137,15 @@ Get a description of the current trial
### Example ### Example
```javascript ```javascript
var trial = jsPsych.currentTrial(); var trial = jsPsych.currentTrial();
console.log('The current trial is using the '+trial.type+' plugin'); console.log('The current trial is using the '+trial.type+' plugin');
``` ```
--- ---
## jsPsych.endCurrentTimeline ## jsPsych.endCurrentTimeline
``` ```javascript
jsPsych.endCurrentTimeline jsPsych.endCurrentTimeline()
``` ```
### Parameters ### Parameters
@ -186,7 +165,6 @@ Ends the current timeline. If timelines are nested, then only the timeline that
#### End timeline if a particular key is pressed #### End timeline if a particular key is pressed
```javascript ```javascript
var images = [ var images = [
"img/1.gif", "img/2.gif", "img/3.gif", "img/4.gif", "img/1.gif", "img/2.gif", "img/3.gif", "img/4.gif",
"img/5.gif", "img/6.gif", "img/7.gif", "img/8.gif", "img/5.gif", "img/6.gif", "img/7.gif", "img/8.gif",
@ -223,13 +201,12 @@ jsPsych.init({
jsPsych.data.displayData(); jsPsych.data.displayData();
} }
}); });
``` ```
--- ---
## jsPsych.endExperiment ## jsPsych.endExperiment
``` ```javascript
jsPsych.endExperiment(end_message) jsPsych.endExperiment(end_message)
``` ```
@ -268,7 +245,7 @@ var trial = {
--- ---
## jsPsych.finishTrial ## jsPsych.finishTrial
``` ```javascript
jsPsych.finishTrial(data) jsPsych.finishTrial(data)
``` ```
@ -297,15 +274,14 @@ This method tells jsPsych that the current trial is over. It is used in all of t
### Example ### Example
```javascript ```javascript
// this code would be in a plugin // this code would be in a plugin
jsPsych.finishTrial({correct_response: true}); jsPsych.finishTrial({correct_response: true});
``` ```
--- ---
## jsPsych.getDisplayElement ## jsPsych.getDisplayElement
``` ```javascript
jsPsych.getDisplayElement() jsPsych.getDisplayElement()
``` ```
@ -333,7 +309,7 @@ el.style.visibility = 'hidden';
--- ---
## jsPsych.getProgressBarCompleted ## jsPsych.getProgressBarCompleted
``` ```javascript
jsPsych.getProgressBarCompleted() jsPsych.getProgressBarCompleted()
``` ```
@ -358,7 +334,7 @@ var progress_bar_amount = jsPsych.getProgressBarCompleted();
--- ---
## jsPsych.init ## jsPsych.init
``` ```javascript
jsPsych.init(settings) jsPsych.init(settings)
``` ```
@ -413,9 +389,10 @@ This method configures and starts the experiment.
See any of the plugin examples in the [examples folder](https://github.com/jodeleeuw/jsPsych/tree/master/examples) in the GitHub repository. See any of the plugin examples in the [examples folder](https://github.com/jodeleeuw/jsPsych/tree/master/examples) in the GitHub repository.
--- ---
## jsPsych.initSettings ## jsPsych.initSettings
``` ```javascript
jsPsych.initSettings() jsPsych.initSettings()
``` ```
@ -441,8 +418,10 @@ console.log(JSON.stringify(settings.timeline));
``` ```
--- ---
## jsPsych.pauseExperiment ## jsPsych.pauseExperiment
```
```javascript
jsPsych.pauseExperiment() jsPsych.pauseExperiment()
``` ```
@ -475,9 +454,10 @@ var trial = {
``` ```
--- ---
## jsPsych.progress ## jsPsych.progress
``` ```javascript
jsPsych.progress() jsPsych.progress()
``` ```
@ -503,15 +483,15 @@ This method returns information about the length of the experiment and the subje
### Example ### Example
```javascript ```javascript
var progress = jsPsych.progress(); var progress = jsPsych.progress();
alert('You have completed approximately '+progress.percent_complete+'% of the experiment'); alert('You have completed approximately '+progress.percent_complete+'% of the experiment');
```
```
--- ---
## jsPsych.resumeExperiment ## jsPsych.resumeExperiment
```
```javascript
jsPsych.resumeExperiment() jsPsych.resumeExperiment()
``` ```
@ -544,9 +524,10 @@ var trial = {
``` ```
--- ---
## jsPsych.setProgressBar ## jsPsych.setProgressBar
``` ```javascript
jsPsych.setProgressBar(value) jsPsych.setProgressBar(value)
``` ```
@ -572,9 +553,10 @@ jsPsych.setProgressBar(0.85);
``` ```
--- ---
## jsPsych.startTime ## jsPsych.startTime
``` ```javascript
jsPsych.startTime() jsPsych.startTime()
``` ```
@ -597,21 +579,20 @@ var start_time = jsPsych.startTime();
``` ```
--- ---
## jsPsych.timelineVariable ## jsPsych.timelineVariable
``` ```javascript
jsPsych.timelineVariable(variable, call_immediate) jsPsych.timelineVariable(variable, call_immediate)
``` ```
### Parameters ### Parameters
Parameter | Type | Description Parameter | Type | Description
----------|------|------------ ----------|------|------------
variable | string | Name of the timeline variable variable | string | Name of the timeline variable
call_immediate | bool | This parameter is optional and can usually be omitted. It determines the return value of `jsPsych.timelineVariable`. If `true`, the function returns the _value_ of the current timeline variable. If `false`, the function returns _a function that returns the value_ of the current timeline variable. When `call_immediate` is omitted, the appropriate option is determined automatically based on the context in which this function is called. When `jsPsych.timelineVariable` is used as a parameter value, `call_immediate` will be `false`. This allows it to be used as a [dynamic trial parameter](/overview/dynamic-parameters). When `jsPsych.timelineVariable` is used inside of a function, `call_immediate` will be `true`. It is possible to explicitly set this option to `true` to force the function to immediately return the current value of the timeline variable. call_immediate | bool | This parameter is optional and can usually be omitted. It determines the return value of `jsPsych.timelineVariable`. If `true`, the function returns the _value_ of the current timeline variable. If `false`, the function returns _a function that returns the value_ of the current timeline variable. When `call_immediate` is omitted, the appropriate option is determined automatically based on the context in which this function is called. When `jsPsych.timelineVariable` is used as a parameter value, `call_immediate` will be `false`. This allows it to be used as a [dynamic trial parameter](/overview/dynamic-parameters). When `jsPsych.timelineVariable` is used inside of a function, `call_immediate` will be `true`. It is possible to explicitly set this option to `true` to force the function to immediately return the current value of the timeline variable.
### Return value ### Return value
Either a function that returns the value of the timeline variable, or the value of the timeline variable, depending on the context in which it is used. See `call_immediate` description above. Either a function that returns the value of the timeline variable, or the value of the timeline variable, depending on the context in which it is used. See `call_immediate` description above.
@ -623,6 +604,7 @@ Either a function that returns the value of the timeline variable, or the value
### Examples ### Examples
#### Standard use as a parameter for a trial #### Standard use as a parameter for a trial
```javascript ```javascript
var trial = { var trial = {
type: 'image-keyboard-response', type: 'image-keyboard-response',
@ -641,6 +623,7 @@ var procedure = {
``` ```
#### Invoking immediately in a function #### Invoking immediately in a function
```javascript ```javascript
var trial = { var trial = {
type: 'html-keyboard-response', type: 'html-keyboard-response',
@ -659,7 +642,9 @@ var procedure = {
] ]
} }
``` ```
Prior to jsPsych v6.3.0, the `call_immediate` parameter must be set to `true` when `jsPsych.timelineVariable` is called from within a function, such as a [dynamic parameter](/overview/dynamic-parameters): Prior to jsPsych v6.3.0, the `call_immediate` parameter must be set to `true` when `jsPsych.timelineVariable` is called from within a function, such as a [dynamic parameter](/overview/dynamic-parameters):
```javascript ```javascript
var trial = { var trial = {
type: 'html-keyboard-response', type: 'html-keyboard-response',
@ -680,9 +665,10 @@ var procedure = {
``` ```
--- ---
## jsPsych.totalTime ## jsPsych.totalTime
``` ```javascript
jsPsych.totalTime() jsPsych.totalTime()
``` ```
@ -701,17 +687,16 @@ Gets the total time the subject has been in the experiment.
### Example ### Example
```javascript ```javascript
var time = jsPsych.totalTime(); var time = jsPsych.totalTime();
console.log(time); console.log(time);
``` ```
--- ---
## jsPsych.version ## jsPsych.version
``` ```javascript
jsPsych.version jsPsych.version()
``` ```
### Parameters ### Parameters

View File

@ -3,9 +3,10 @@
The jsPsych.data module contains functions for interacting with the data generated by jsPsych plugins. The jsPsych.data module contains functions for interacting with the data generated by jsPsych plugins.
--- ---
## jsPsych.data.addProperties ## jsPsych.data.addProperties
``` ```javascript
jsPsych.data.addProperties(properties) jsPsych.data.addProperties(properties)
``` ```
@ -23,19 +24,19 @@ Returns nothing.
This method appends a set of properties to every trial in the data object, including trials that have already occurred and trials that have yet to occur. You can use this to record things like the subject ID or condition assignment. This method appends a set of properties to every trial in the data object, including trials that have already occurred and trials that have yet to occur. You can use this to record things like the subject ID or condition assignment.
### Examples ### Examples
#### Assigning a subject ID and condition code #### Assigning a subject ID and condition code
```javascript ```javascript
jsPsych.data.addProperties({subject: 1, condition: 'control'}); jsPsych.data.addProperties({subject: 1, condition: 'control'});
``` ```
--- ---
## jsPsych.data.displayData ## jsPsych.data.displayData
``` ```javascript
jsPsych.data.displayData(format) jsPsych.data.displayData(format)
``` ```
@ -56,6 +57,7 @@ Outputs all of the data collected in the experiment to the screen in either JSON
### Examples ### Examples
#### Using the on_finish callback function to show data at the end of the experiment #### Using the on_finish callback function to show data at the end of the experiment
```javascript ```javascript
jsPsych.init({ jsPsych.init({
experiment_structure: exp, experiment_structure: exp,
@ -66,6 +68,7 @@ jsPsych.init({
``` ```
--- ---
## jsPsych.data.get ## jsPsych.data.get
``` ```
@ -97,9 +100,10 @@ console.log(all_data.csv());
``` ```
--- ---
## jsPsych.data.getDataByTimelineNode ## jsPsych.data.getDataByTimelineNode
``` ```javascript
jsPsych.data.getDataByTimelineNode(node_id) jsPsych.data.getDataByTimelineNode(node_id)
``` ```
@ -120,17 +124,15 @@ Get all the data generated by a specified Timeline.
### Example ### Example
```javascript ```javascript
var current_node_id = jsPsych.currentTimelineNodeID(); var current_node_id = jsPsych.currentTimelineNodeID();
var data_from_current_node = jsPsych.data.getDataByTimelineNode(current_node_id); var data_from_current_node = jsPsych.data.getDataByTimelineNode(current_node_id);
``` ```
--- ---
## jsPsych.data.getInteractionData ## jsPsych.data.getInteractionData
``` ```javascript
jsPsych.data.getInteractionData() jsPsych.data.getInteractionData()
``` ```
@ -162,11 +164,11 @@ var interaction_data = jsPsych.data.getInteractionData();
console.log(interaction_data.json()); console.log(interaction_data.json());
``` ```
--- ---
## jsPsych.data.getLastTimelineData ## jsPsych.data.getLastTimelineData
``` ```javascript
jsPsych.data.getLastTimelineData() jsPsych.data.getLastTimelineData()
``` ```
@ -180,20 +182,21 @@ Gets all of the data generated in the same timeline as the last trial.
### Example ### Example
```js ```javascript
var lasttimelinedata = jsPsych.data.getLastTimelineData(); var lasttimelinedata = jsPsych.data.getLastTimelineData();
``` ```
--- ---
## jsPsych.data.getLastTrialData ## jsPsych.data.getLastTrialData
``` ```javascript
jsPsych.data.getLastTrialData() jsPsych.data.getLastTrialData()
``` ```
### Return value ### Return value
Returns a DataCollection Returns a DataCollection.
### Description ### Description
@ -201,14 +204,15 @@ Gets the data collection containing all data generated by the last trial.
### Example ### Example
``` ```javascript
var lasttrialdata = jsPsych.data.getLastTrialData(); var lasttrialdata = jsPsych.data.getLastTrialData();
``` ```
--- ---
## jsPsych.data.getURLVariable ## jsPsych.data.getURLVariable
``` ```javascript
jsPsych.data.getURLVariable(var_name) jsPsych.data.getURLVariable(var_name)
``` ```
@ -226,21 +230,19 @@ Returns the value of a variable passed in through the query string.
For extracting a particular variable passed in through a URL query string. For extracting a particular variable passed in through a URL query string.
### Examples ### Example
```javascript ```javascript
// if the URL of the page is: experiment.html?subject=1234&condition=test // if the URL of the page is: experiment.html?subject=1234&condition=test
console.log(jsPsych.data.getURLVariable('subject')) // logs "1234" console.log(jsPsych.data.getURLVariable('subject')) // logs "1234"
console.log(jsPsych.data.getURLVariable('condition')) // logs "test" console.log(jsPsych.data.getURLVariable('condition')) // logs "test"
``` ```
--- ---
## jsPsych.data.urlVariables ## jsPsych.data.urlVariables
``` ```javascript
jsPsych.data.urlVariables() jsPsych.data.urlVariables()
``` ```
@ -252,23 +254,20 @@ Returns an object (associative array) of the variables in the URL query string.
For extracting variables passed in through a URL query string. For extracting variables passed in through a URL query string.
### Examples ### Example
```javascript ```javascript
// if the URL of the page is: experiment.html?subject=1234&condition=test // if the URL of the page is: experiment.html?subject=1234&condition=test
var urlvar = jsPsych.data.urlVariables(); var urlvar = jsPsych.data.urlVariables();
console.log(urlvar.subject) // logs "1234" console.log(urlvar.subject) // logs "1234"
console.log(urlvar.condition) // logs "test" console.log(urlvar.condition) // logs "test"
``` ```
--- ---
## jsPsych.data.write ## jsPsych.data.write
``` ```javascript
jsPsych.data.write(data_object) jsPsych.data.write(data_object)
``` ```
@ -286,10 +285,9 @@ Returns nothing.
This method is used by `jsPsych.finishTrial` for writing data. You should probably not use it to add data. Instead use [jsPsych.data.addProperties](#addProperties). This method is used by `jsPsych.finishTrial` for writing data. You should probably not use it to add data. Instead use [jsPsych.data.addProperties](#addProperties).
### Examples ### Example
```javascript ```javascript
// don't use this! data should only be written once per trial. use jsPsych.finishTrial to save data. // don't use this! data should only be written once per trial. use jsPsych.finishTrial to save data.
var trial_data = { var trial_data = {
@ -298,20 +296,19 @@ var trial_data = {
} }
jsPsych.data.write(trial_data); jsPsych.data.write(trial_data);
``` ```
--- ---
## DataCollection ## DataCollection
All data is stored in the DataCollection object. Using methods like `jsPsych.data.get()` and `jsPsych.data.getLastTrialData()` return DataCollections containing All data is stored in the DataCollection object. Using methods like `jsPsych.data.get()` and `jsPsych.data.getLastTrialData()` return DataCollections containing the experiment data. This is a list of all of the methods that are available to call on a DataCollection object.
the experiment data. This is a list of all of the methods that are available to call on a DataCollection object.
#### .addToAll() #### .addToAll()
Adds a set of properties to all items in the DataCollection. Similar to `jsPsych.data.addProperties()`, except that it can be applied to a subset of the whole DataCollection by filtering down to a smaller DataCollection first. Adds a set of properties to all items in the DataCollection. Similar to `jsPsych.data.addProperties()`, except that it can be applied to a subset of the whole DataCollection by filtering down to a smaller DataCollection first.
```js ```javascript
jsPsych.data.get().addToAll({subject_id: 123, condition: 'control'}); jsPsych.data.get().addToAll({subject_id: 123, condition: 'control'});
``` ```
@ -319,7 +316,7 @@ jsPsych.data.get().addToAll({subject_id: 123, condition: 'control'});
Adds a set of properties to the last trial in the DataCollection. Adds a set of properties to the last trial in the DataCollection.
```js ```javascript
jsPsych.data.get().addToLast({success: true}); jsPsych.data.get().addToLast({success: true});
``` ```
@ -327,7 +324,7 @@ jsPsych.data.get().addToLast({success: true});
Counts the number of trials in the DataCollection. Counts the number of trials in the DataCollection.
```js ```javascript
jsPsych.data.get().count() jsPsych.data.get().count()
``` ```
@ -335,7 +332,7 @@ jsPsych.data.get().count()
Generates a CSV string representing all of the data in the DataCollection. Generates a CSV string representing all of the data in the DataCollection.
```js ```javascript
console.log(jsPsych.data.get().csv()); console.log(jsPsych.data.get().csv());
``` ```
@ -343,27 +340,27 @@ console.log(jsPsych.data.get().csv());
Returns a subset of the DataCollection based on the filter. The filter is an object, and trials are only kept in the returned DataCollection if they contain the key: value pair(s) in the filter object. For example, the code below selects all of the trials with a correct response. Returns a subset of the DataCollection based on the filter. The filter is an object, and trials are only kept in the returned DataCollection if they contain the key: value pair(s) in the filter object. For example, the code below selects all of the trials with a correct response.
```js ```javascript
var correct_trials = jsPsych.data.get().filter({correct: true}); var correct_trials = jsPsych.data.get().filter({correct: true});
``` ```
The object can have multiple key: value pairs, and the trials must match all of them in order to be included in the returned collection. The object can have multiple key: value pairs, and the trials must match all of them in order to be included in the returned collection.
```js ```javascript
// keep only correct trials from the practice phase // keep only correct trials from the practice phase
var correct_practice_trials = jsPsych.data.get().filter({correct:true, phase: 'practice'}); var correct_practice_trials = jsPsych.data.get().filter({correct:true, phase: 'practice'});
``` ```
The filter can also be an array of objects. In this case each object in the array acts as an OR filter. As long as the trial has all the key: value pairs of one of the objects in the array, it will appear in the returned collection. The filter can also be an array of objects. In this case each object in the array acts as an OR filter. As long as the trial has all the key: value pairs of one of the objects in the array, it will appear in the returned collection.
```js ```javascript
// select trials from block 1 and block 5. // select trials from block 1 and block 5.
var trials = jsPsych.data.get().filter([{block: 1}, {block:5}]); var trials = jsPsych.data.get().filter([{block: 1}, {block:5}]);
``` ```
The filter method returns a DataCollection object, so methods can be chained onto a single statement. The filter method returns a DataCollection object, so methods can be chained onto a single statement.
```js ```javascript
// count the number of correct trials in block 1 // count the number of correct trials in block 1
var block_1_correct = jsPsych.data.get().filter({block:1, correct:true}).count(); var block_1_correct = jsPsych.data.get().filter({block:1, correct:true}).count();
``` ```
@ -372,7 +369,7 @@ var block_1_correct = jsPsych.data.get().filter({block:1, correct:true}).count()
This method is similar to the `.filter()` method, except that it accepts a function as the filter. The function is passed a single argument, containing the data for a trial. If the function returns `true` the trial is included in the returned DataCollection. This method is similar to the `.filter()` method, except that it accepts a function as the filter. The function is passed a single argument, containing the data for a trial. If the function returns `true` the trial is included in the returned DataCollection.
```js ```javascript
// count the number of trials with a response time greater than 2000ms. // count the number of trials with a response time greater than 2000ms.
var too_long = jsPsych.data.get().filterCustom(function(trial){ var too_long = jsPsych.data.get().filterCustom(function(trial){
return trial.rt > 2000; return trial.rt > 2000;
@ -383,7 +380,7 @@ var too_long = jsPsych.data.get().filterCustom(function(trial){
Returns a DataCollection containing the first/last *n* trials. If *n* is greater than the number of trials in the DataCollection, then these functions will return an array of length equal to the number of trials. If there are no trials in the DataCollection, then these functions will return an empty array. If the *n* argument is omitted, then the functions will use the default value of 1. If *n* is zero or a negative number, then these functions will throw an error. Returns a DataCollection containing the first/last *n* trials. If *n* is greater than the number of trials in the DataCollection, then these functions will return an array of length equal to the number of trials. If there are no trials in the DataCollection, then these functions will return an empty array. If the *n* argument is omitted, then the functions will use the default value of 1. If *n* is zero or a negative number, then these functions will throw an error.
```js ```javascript
var first_trial = jsPsych.data.get().first(1); var first_trial = jsPsych.data.get().first(1);
var last_trial_with_correct_response = jsPsych.data.get().filter({correct: true}).last(1); var last_trial_with_correct_response = jsPsych.data.get().filter({correct: true}).last(1);
var last_10_trials = jsPsych.data.get().last(10); var last_10_trials = jsPsych.data.get().last(10);
@ -393,7 +390,7 @@ var last_10_trials = jsPsych.data.get().last(10);
Returns a DataCollection with all instances of a particular key removed from the dataset. Returns a DataCollection with all instances of a particular key removed from the dataset.
```js ```javascript
// log a csv file that does not contain the internal_node_id values for each trial // log a csv file that does not contain the internal_node_id values for each trial
console.log(jsPsych.data.get().ignore('internal_node_id').csv()); console.log(jsPsych.data.get().ignore('internal_node_id').csv());
``` ```
@ -402,7 +399,7 @@ console.log(jsPsych.data.get().ignore('internal_node_id').csv());
Appends one DataCollection onto another and returns the combined collection. Appends one DataCollection onto another and returns the combined collection.
```js ```javascript
// get a DataCollection with all trials that are either correct or // get a DataCollection with all trials that are either correct or
// have a response time greater than 200ms. // have a response time greater than 200ms.
var dc1 = jsPsych.data.get().filter({correct: true}); var dc1 = jsPsych.data.get().filter({correct: true});
@ -414,7 +411,7 @@ var data = dc1.join(dc2);
Generates a JSON string representing all of the data in the DataCollection. Generates a JSON string representing all of the data in the DataCollection.
```js ```javascript
console.log(jsPsych.data.get().json()); console.log(jsPsych.data.get().json());
``` ```
@ -434,7 +431,7 @@ jsPsych.data.get().localSave('csv','mydata.csv');
Add a new entry to the DataCollection. This method is mostly used internally, and you shouldn't need to call it under normal circumstances. Add a new entry to the DataCollection. This method is mostly used internally, and you shouldn't need to call it under normal circumstances.
```js ```javascript
var data = {correct: true, rt: 500} var data = {correct: true, rt: 500}
jsPsych.data.get().push(data); jsPsych.data.get().push(data);
``` ```
@ -443,7 +440,7 @@ jsPsych.data.get().push(data);
Creates a copy of the DataCollection so that any modification of the values in the DataCollection will not affect the original. Creates a copy of the DataCollection so that any modification of the values in the DataCollection will not affect the original.
```js ```javascript
// this line edits the rt property of the first trial // this line edits the rt property of the first trial
jsPsych.data.get().first(1).values()[0].rt = 100; jsPsych.data.get().first(1).values()[0].rt = 100;
@ -461,7 +458,7 @@ jsPsych.data.get().first(1).values()[0].rt
Returns a DataColumn object (see documentation below) of a single property from a DataCollection object. Returns a DataColumn object (see documentation below) of a single property from a DataCollection object.
```js ```javascript
var rt_data = jsPsych.data.get().select('rt'); var rt_data = jsPsych.data.get().select('rt');
rt_data.mean() rt_data.mean()
``` ```
@ -470,7 +467,7 @@ rt_data.mean()
Generates an array of all the unique key names in the set of trials contained in the DataCollection. This is especially useful when setting up a relational database (e.g., MySQL) where the column names need to be specified in advance. Generates an array of all the unique key names in the set of trials contained in the DataCollection. This is especially useful when setting up a relational database (e.g., MySQL) where the column names need to be specified in advance.
```js ```javascript
console.log(jsPsych.data.get().uniqueNames()); console.log(jsPsych.data.get().uniqueNames());
``` ```
@ -478,7 +475,7 @@ console.log(jsPsych.data.get().uniqueNames());
Returns the raw data array associated with the DataCollection. This array is modifiable, so changes to the array and values of objects in the array will change the DataCollection. Returns the raw data array associated with the DataCollection. This array is modifiable, so changes to the array and values of objects in the array will change the DataCollection.
```js ```javascript
var raw_data = jsPsych.data.get().values(); var raw_data = jsPsych.data.get().values();
// was response in first trial correct? // was response in first trial correct?
@ -490,6 +487,7 @@ if(raw_data[0].correct){
``` ```
--- ---
## DataColumn ## DataColumn
DataColumn objects represent all the values of a single property in a DataCollection. They are generated by using the `.select()` method on a DataCollection. Once a DataColumn is generated, the following methods can be used. DataColumn objects represent all the values of a single property in a DataCollection. They are generated by using the `.select()` method on a DataCollection. Once a DataColumn is generated, the following methods can be used.
@ -498,7 +496,7 @@ DataColumn objects represent all the values of a single property in a DataCollec
Checks if all values in the DataColumn return `true` when passed to a function. The function takes a single argument, which represents one value from the DataColumn. Checks if all values in the DataColumn return `true` when passed to a function. The function takes a single argument, which represents one value from the DataColumn.
```js ```javascript
// check if all the response times in the practice phase were under 1000ms // check if all the response times in the practice phase were under 1000ms
jsPsych.data.get().filter({phase: 'practice'}).select('correct').all(function(x) { return x < 1000; }); jsPsych.data.get().filter({phase: 'practice'}).select('correct').all(function(x) { return x < 1000; });
``` ```
@ -507,7 +505,7 @@ jsPsych.data.get().filter({phase: 'practice'}).select('correct').all(function(x)
Counts the number of values in the DataColumn. Counts the number of values in the DataColumn.
```js ```javascript
// count how many response times there are // count how many response times there are
jsPsych.data.get().select('rt').count(); jsPsych.data.get().select('rt').count();
``` ```
@ -516,7 +514,7 @@ jsPsych.data.get().select('rt').count();
Counts the number of occurrences of each unique value in the DataColumn. Returns this value as an object, where each key is a unique value and the value of each key is the number of occurrences of that key. Counts the number of occurrences of each unique value in the DataColumn. Returns this value as an object, where each key is a unique value and the value of each key is the number of occurrences of that key.
```js ```javascript
// get frequencies of correct and incorrect responses // get frequencies of correct and incorrect responses
jsPsych.data.get().select('correct').frequencies(); jsPsych.data.get().select('correct').frequencies();
``` ```
@ -525,7 +523,7 @@ jsPsych.data.get().select('correct').frequencies();
Returns the maximum or minimum value in a DataColumn. Returns the maximum or minimum value in a DataColumn.
```js ```javascript
jsPsych.data.get().select('rt').max(); jsPsych.data.get().select('rt').max();
jsPsych.data.get().select('rt').min(); jsPsych.data.get().select('rt').min();
``` ```
@ -534,7 +532,7 @@ jsPsych.data.get().select('rt').min();
Returns the average of all the values in a DataColumn. Returns the average of all the values in a DataColumn.
```js ```javascript
jsPsych.data.get().select('rt').mean(); jsPsych.data.get().select('rt').mean();
``` ```
@ -542,7 +540,7 @@ jsPsych.data.get().select('rt').mean();
Returns the median of all the values in a DataColumn. Returns the median of all the values in a DataColumn.
```js ```javascript
jsPsych.data.get().select('rt').median(); jsPsych.data.get().select('rt').median();
``` ```
@ -550,7 +548,7 @@ jsPsych.data.get().select('rt').median();
Returns the standard deviation of the values in a DataColumn. Returns the standard deviation of the values in a DataColumn.
```js ```javascript
jsPsych.data.get().select('rt').sd(); jsPsych.data.get().select('rt').sd();
``` ```
@ -558,7 +556,7 @@ jsPsych.data.get().select('rt').sd();
Filters the DataColumn to include only values that return `true` when passed through the specified function. Filters the DataColumn to include only values that return `true` when passed through the specified function.
```js ```javascript
// below results will be less than 200. // below results will be less than 200.
jsPsych.data.get().select('rt').subset(function(x){ return x < 200; }).max(); jsPsych.data.get().select('rt').subset(function(x){ return x < 200; }).max();
``` ```
@ -567,7 +565,7 @@ jsPsych.data.get().select('rt').subset(function(x){ return x < 200; }).max();
Returns the sum of the values in a DataColumn. Returns the sum of the values in a DataColumn.
```js ```javascript
jsPsych.data.get().select('rt').sum(); jsPsych.data.get().select('rt').sum();
``` ```
@ -575,7 +573,7 @@ jsPsych.data.get().select('rt').sum();
The raw array of values in the DataColumn. The raw array of values in the DataColumn.
```js ```javascript
// note that this is not a function call. // note that this is not a function call.
jsPsych.data.get().select('rt').values; jsPsych.data.get().select('rt').values;
``` ```
@ -584,6 +582,6 @@ jsPsych.data.get().select('rt').values;
Returns the variance of the values in a DataColumn. Returns the variance of the values in a DataColumn.
```js ```javascript
jsPsych.data.get().select('rt').variance(); jsPsych.data.get().select('rt').variance();
``` ```

View File

@ -3,9 +3,10 @@
The pluginAPI module contains functions that are useful when developing new plugins. The pluginAPI module contains functions that are useful when developing new plugins.
--- ---
## jsPsych.pluginAPI.cancelAllKeyboardResponses ## jsPsych.pluginAPI.cancelAllKeyboardResponses
``` ```javascript
jsPsych.pluginAPI.cancelAllKeyboardResponses() jsPsych.pluginAPI.cancelAllKeyboardResponses()
``` ```
@ -21,16 +22,17 @@ Returns nothing.
Cancels all currently active keyboard listeners created by `jsPsych.pluginAPI.getKeyboardResponse`. Cancels all currently active keyboard listeners created by `jsPsych.pluginAPI.getKeyboardResponse`.
### Examples ### Example
```javascript ```javascript
jsPsych.pluginAPI.cancelAllKeyboardResponses(); jsPsych.pluginAPI.cancelAllKeyboardResponses();
``` ```
--- ---
## jsPsych.pluginAPI.cancelKeyboardResponse ## jsPsych.pluginAPI.cancelKeyboardResponse
``` ```javascript
jsPsych.pluginAPI.cancelKeyboardResponse(listener_id) jsPsych.pluginAPI.cancelKeyboardResponse(listener_id)
``` ```
@ -48,8 +50,7 @@ Returns nothing.
Cancels a specific keyboard listener created by `jsPsych.pluginAPI.getKeyboardResponse`. Cancels a specific keyboard listener created by `jsPsych.pluginAPI.getKeyboardResponse`.
### Example
### Examples
```javascript ```javascript
// create a persistent keyboard listener // create a persistent keyboard listener
@ -66,9 +67,10 @@ jsPsych.pluginAPI.cancelKeyboardResponse(listener_id);
``` ```
--- ---
## jsPsych.pluginAPI.clearAllTimeouts ## jsPsych.pluginAPI.clearAllTimeouts
``` ```javascript
jsPsych.pluginAPI.clearAllTimeouts() jsPsych.pluginAPI.clearAllTimeouts()
``` ```
@ -82,12 +84,13 @@ Returns nothing.
### Description ### Description
Clears any pending timeouts that were set using jsPsych.pluginAPI.setTimeout() Clears any pending timeouts that were set using jsPsych.pluginAPI.setTimeout().
--- ---
## jsPsych.pluginAPI.compareKeys ## jsPsych.pluginAPI.compareKeys
``` ```javascript
jsPsych.pluginAPI.compareKeys(key1, key2) jsPsych.pluginAPI.compareKeys(key1, key2)
``` ```
@ -113,6 +116,7 @@ We recommend using this function to compare keys in all plugin and experiment co
### Examples ### Examples
#### Basic examples #### Basic examples
```javascript ```javascript
jsPsych.pluginAPI.compareKeys('a', 'A'); jsPsych.pluginAPI.compareKeys('a', 'A');
// returns true when case_sensitive_responses is false in jsPsych.init // returns true when case_sensitive_responses is false in jsPsych.init
@ -129,6 +133,7 @@ jsPsych.pluginAPI.compareKeys('space', 31);
``` ```
#### Comparing a key response and key parameter value in plugins #### Comparing a key response and key parameter value in plugins
```javascript ```javascript
// this is the callback_function passed to jsPsych.pluginAPI.getKeyboardResponse // this is the callback_function passed to jsPsych.pluginAPI.getKeyboardResponse
var after_response = function(info) { var after_response = function(info) {
@ -139,6 +144,7 @@ var after_response = function(info) {
``` ```
#### Scoring a key response in experiment code #### Scoring a key response in experiment code
```javascript ```javascript
var trial = { var trial = {
type: 'html-keyboard-response', type: 'html-keyboard-response',
@ -158,9 +164,10 @@ var trial = {
``` ```
--- ---
## jsPsych.pluginAPI.getAudioBuffer ## jsPsych.pluginAPI.getAudioBuffer
``` ```javascript
jsPsych.pluginAPI.getAudioBuffer(filepath) jsPsych.pluginAPI.getAudioBuffer(filepath)
``` ```
@ -183,6 +190,7 @@ It is strongly recommended that you preload audio files before calling this meth
### Examples ### Examples
#### HTML 5 Audio #### HTML 5 Audio
```javascript ```javascript
jsPsych.pluginAPI.getAudioBuffer('my-sound.mp3') jsPsych.pluginAPI.getAudioBuffer('my-sound.mp3')
.then(function(audio){ .then(function(audio){
@ -194,6 +202,7 @@ jsPsych.pluginAPI.getAudioBuffer('my-sound.mp3')
``` ```
#### WebAudio API #### WebAudio API
```javascript ```javascript
var context = jsPsych.pluginAPI.audioContext(); var context = jsPsych.pluginAPI.audioContext();
@ -212,9 +221,10 @@ jsPsych.pluginAPI.getAudioBuffer('my-sound.mp3')
See the `audio-keyboard-response` plugin for an example in a fuller context. See the `audio-keyboard-response` plugin for an example in a fuller context.
--- ---
## jsPsych.pluginAPI.getAutoPreloadList ## jsPsych.pluginAPI.getAutoPreloadList
``` ```javascript
jsPsych.pluginAPI.getAutoPreloadList(timeline) jsPsych.pluginAPI.getAutoPreloadList(timeline)
``` ```
@ -258,9 +268,10 @@ jsPsych.pluginAPI.getAutoPreloadList(timeline);
``` ```
--- ---
## jsPsych.pluginAPI.getKeyboardResponse ## jsPsych.pluginAPI.getKeyboardResponse
``` ```javascript
jsPsych.pluginAPI.getKeyboardResponse(parameters) jsPsych.pluginAPI.getKeyboardResponse(parameters)
``` ```
@ -295,6 +306,7 @@ This function uses the `.key` value of the keyboard event, which is _case sensit
### Examples ### Examples
#### Get a single response from any key #### Get a single response from any key
```javascript ```javascript
var after_response = function(info){ var after_response = function(info){
@ -310,6 +322,7 @@ jsPsych.pluginAPI.getKeyboardResponse({
``` ```
#### Get a responses from a key until the letter q is pressed #### Get a responses from a key until the letter q is pressed
```javascript ```javascript
var after_response = function(info){ var after_response = function(info){
@ -329,9 +342,10 @@ var listener = jsPsych.pluginAPI.getKeyboardResponse({
``` ```
--- ---
## jsPsych.pluginAPI.preloadAudio ## jsPsych.pluginAPI.preloadAudio
``` ```javascript
jsPsych.pluginAPI.preloadAudio(files, callback_complete, callback_load, callback_error) jsPsych.pluginAPI.preloadAudio(files, callback_complete, callback_load, callback_error)
``` ```
@ -359,8 +373,8 @@ The `callback_load` and `callback_error` functions are called after each file ha
### Examples ### Examples
#### Basic use #### Basic use
```javascript
```javascript
var sounds = ['file1.mp3', 'file2.mp3', 'file3.mp3']; var sounds = ['file1.mp3', 'file2.mp3', 'file3.mp3'];
jsPsych.pluginAPI.preloadAudio(sounds, jsPsych.pluginAPI.preloadAudio(sounds,
@ -374,7 +388,6 @@ function startExperiment(){
timeline: exp timeline: exp
}); });
} }
``` ```
#### Show progress of loading #### Show progress of loading
@ -386,7 +399,7 @@ var n_loaded = 0;
jsPsych.pluginAPI.preloadAudio(sounds, function(){ startExperiment(); }, function(file) { updateLoadedCount(file); }); jsPsych.pluginAPI.preloadAudio(sounds, function(){ startExperiment(); }, function(file) { updateLoadedCount(file); });
function updateLoadedCount(file){ function updateLoadedCount(file){
n_loaded++; n_loaded++;
var percentcomplete = n_loaded / sounds.length * 100; var percentcomplete = n_loaded / sounds.length * 100;
// could put something fancier here, like a progress bar // could put something fancier here, like a progress bar
@ -395,16 +408,17 @@ function updateLoadedCount(file){
} }
function startExperiment(){ function startExperiment(){
jsPsych.init({ jsPsych.init({
timeline: exp timeline: exp
}); });
} }
``` ```
--- ---
## jsPsych.pluginAPI.preloadImages ## jsPsych.pluginAPI.preloadImages
``` ```javascript
jsPsych.pluginAPI.preloadImages(images, callback_complete, callback_load, callback_error) jsPsych.pluginAPI.preloadImages(images, callback_complete, callback_load, callback_error)
``` ```
@ -432,8 +446,8 @@ The `callback_load` and `callback_error` functions are called after each file ha
### Examples ### Examples
#### Basic use #### Basic use
```javascript
```javascript
var images = ['img/file1.png', 'img/file2.png', 'img/file3.png']; var images = ['img/file1.png', 'img/file2.png', 'img/file3.png'];
jsPsych.pluginAPI.preloadImages(images, jsPsych.pluginAPI.preloadImages(images,
@ -447,7 +461,6 @@ function startExperiment(){
timeline: exp timeline: exp
}); });
} }
``` ```
#### Show progress of loading #### Show progress of loading
@ -459,7 +472,7 @@ var n_loaded = 0;
jsPsych.pluginAPI.preloadImages(images, function(){ startExperiment(); }, function(file) { updateLoadedCount(file); }); jsPsych.pluginAPI.preloadImages(images, function(){ startExperiment(); }, function(file) { updateLoadedCount(file); });
function updateLoadedCount(file){ function updateLoadedCount(file){
n_loaded++; n_loaded++;
var percentcomplete = n_loaded / images.length * 100; var percentcomplete = n_loaded / images.length * 100;
// could put something fancier here, like a progress bar // could put something fancier here, like a progress bar
@ -468,16 +481,17 @@ function updateLoadedCount(file){
} }
function startExperiment(){ function startExperiment(){
jsPsych.init({ jsPsych.init({
timeline: exp timeline: exp
}); });
} }
``` ```
--- ---
## jsPsych.pluginAPI.preloadVideo ## jsPsych.pluginAPI.preloadVideo
``` ```javascript
jsPsych.pluginAPI.preloadVideo(video, callback_complete, callback_load, callback_error) jsPsych.pluginAPI.preloadVideo(video, callback_complete, callback_load, callback_error)
``` ```
@ -505,22 +519,21 @@ The `callback_load` and `callback_error` functions are called after each file ha
### Examples ### Examples
#### Basic use #### Basic use
```javascript
```javascript
var videos = ['vid/file1.mp4', 'vid/file2.mp4', 'vid/file3.mp4']; var videos = ['vid/file1.mp4', 'vid/file2.mp4', 'vid/file3.mp4'];
jsPsych.pluginAPI.preloadVideo(videos, jsPsych.pluginAPI.preloadVideo(videos,
function(){ startExperiment(); }, function(){ startExperiment(); },
function(file){ console.log('file loaded: ', file); } function(file){ console.log('file loaded: ', file); }
function(file){ console.log('error loading file: ', file); } function(file){ console.log('error loading file: ', file); }
); );
function startExperiment(){ function startExperiment(){
jsPsych.init({ jsPsych.init({
timeline: exp timeline: exp
}); });
} }
``` ```
#### Show progress of loading #### Show progress of loading
@ -532,7 +545,7 @@ var n_loaded = 0;
jsPsych.pluginAPI.preloadVideo(videos, function(){ startExperiment(); }, function(file) { updateLoadedCount(file); }); jsPsych.pluginAPI.preloadVideo(videos, function(){ startExperiment(); }, function(file) { updateLoadedCount(file); });
function updateLoadedCount(file){ function updateLoadedCount(file){
n_loaded++; n_loaded++;
var percentcomplete = n_loaded / videos.length * 100; var percentcomplete = n_loaded / videos.length * 100;
// could put something fancier here, like a progress bar // could put something fancier here, like a progress bar
@ -541,16 +554,17 @@ function updateLoadedCount(file){
} }
function startExperiment(){ function startExperiment(){
jsPsych.init({ jsPsych.init({
timeline: exp timeline: exp
}); });
} }
``` ```
--- ---
## jsPsych.pluginAPI.registerPreload ## jsPsych.pluginAPI.registerPreload
``` ```javascript
jsPsych.pluginAPI.registerPreload(plugin_name, parameter, media_type) jsPsych.pluginAPI.registerPreload(plugin_name, parameter, media_type)
``` ```
@ -578,7 +592,7 @@ For an example, see the [image-keyboard-response](https://github.com/jspsych/jsP
## jsPsych.pluginAPI.setTimeout ## jsPsych.pluginAPI.setTimeout
``` ```javascript
jsPsych.pluginAPI.setTimeout(callback, delay) jsPsych.pluginAPI.setTimeout(callback, delay)
``` ```
@ -597,7 +611,7 @@ Returns the ID of the setTimeout handle.
This is simply a call to the standard setTimeout function in JavaScript with the added benefit of registering the setTimeout call in a central list. This is useful for scenarios where some other event (the trial ending, aborting the experiment) should stop the execution of queued timeouts. This is simply a call to the standard setTimeout function in JavaScript with the added benefit of registering the setTimeout call in a central list. This is useful for scenarios where some other event (the trial ending, aborting the experiment) should stop the execution of queued timeouts.
### Examples ### Example
```javascript ```javascript
// print the time // print the time

View File

@ -6,7 +6,7 @@ The jsPsych.randomization module contains methods that are useful for generating
## jsPsych.randomization.factorial ## jsPsych.randomization.factorial
``` ```javascript
jsPsych.randomization.factorial(factors, repetitions, unpack) jsPsych.randomization.factorial(factors, repetitions, unpack)
``` ```
@ -29,6 +29,7 @@ This method takes a list of factors and their levels, and creates a full factori
### Examples ### Examples
#### Create full factorial design #### Create full factorial design
```javascript ```javascript
var factors = { var factors = {
stimulus: ['a.jpg', 'b.jpg'], stimulus: ['a.jpg', 'b.jpg'],
@ -49,6 +50,7 @@ full_design = [
``` ```
#### Create full factorial design with repeats #### Create full factorial design with repeats
```javascript ```javascript
var factors = { var factors = {
stimulus: ['a.jpg', 'b.jpg'], stimulus: ['a.jpg', 'b.jpg'],
@ -73,6 +75,7 @@ full_design = [
``` ```
#### Create full factorial design, unpacked #### Create full factorial design, unpacked
```javascript ```javascript
var factors = { var factors = {
stimulus: ['a.jpg', 'b.jpg'], stimulus: ['a.jpg', 'b.jpg'],
@ -91,9 +94,10 @@ full_design = {
``` ```
--- ---
## jsPsych.randomization.randomID ## jsPsych.randomization.randomID
``` ```javascript
jsPsych.randomization.randomID(length) jsPsych.randomization.randomID(length)
``` ```
@ -114,19 +118,18 @@ Generates a random string that is likely to be unique. If length is undefined, t
### Example ### Example
```javascript ```javascript
console.log(jsPsych.randomization.randomID()); console.log(jsPsych.randomization.randomID());
// outputs: "t7dwz0e713pc8juuaayyfvpkdd9un239" // outputs: "t7dwz0e713pc8juuaayyfvpkdd9un239"
console.log(jsPsych.randomization.randomID(8)); console.log(jsPsych.randomization.randomID(8));
// outputs: "3xtpcbck" // outputs: "3xtpcbck"
``` ```
--- ---
## jsPsych.randomization.repeat ## jsPsych.randomization.repeat
``` ```javascript
jsPsych.randomization.repeat(array, repetitions, unpack) jsPsych.randomization.repeat(array, repetitions, unpack)
``` ```
@ -153,27 +156,22 @@ If the array elements are objects with the same set of properties, then this met
#### Shuffle an array, no repeats #### Shuffle an array, no repeats
```javascript ```javascript
var myArray = [1,2,3,4,5]; var myArray = [1,2,3,4,5];
var shuffledArray = jsPsych.randomization.repeat(myArray, 1); var shuffledArray = jsPsych.randomization.repeat(myArray, 1);
// output: shuffledArray = [3,2,4,1,5] // output: shuffledArray = [3,2,4,1,5]
``` ```
#### Shuffle an array with repeats #### Shuffle an array with repeats
```javascript ```javascript
var myArray = [1,2,3,4,5]; var myArray = [1,2,3,4,5];
var shuffledArray = jsPsych.randomization.repeat(myArray, 2); var shuffledArray = jsPsych.randomization.repeat(myArray, 2);
// output: shuffledArray = [1,3,4,2,2,4,5,1,5,3] // output: shuffledArray = [1,3,4,2,2,4,5,1,5,3]
``` ```
#### Shuffle an array of objects #### Shuffle an array of objects
```javascript ```javascript
var trial1 = { var trial1 = {
stimulus: 'img/faceA.jpg', stimulus: 'img/faceA.jpg',
correct_key: 'p', correct_key: 'p',
@ -201,7 +199,6 @@ var shuffledArray = jsPsych.randomization.repeat(myArray, 2);
#### Shuffle an array of objects, with unpack #### Shuffle an array of objects, with unpack
```javascript ```javascript
var trial1 = { var trial1 = {
stimulus: 'img/faceA.jpg', stimulus: 'img/faceA.jpg',
correct_key: 'p', correct_key: 'p',
@ -231,10 +228,12 @@ output: shuffledArray = {
} }
*/ */
``` ```
--- ---
## jsPsych.randomization.sampleWithReplacement ## jsPsych.randomization.sampleWithReplacement
``` ```javascript
jsPsych.randomization.sampleWithReplacement(array, sampleSize, weights) jsPsych.randomization.sampleWithReplacement(array, sampleSize, weights)
``` ```
@ -259,27 +258,24 @@ This method returns a sample drawn at random from a set of values with replaceme
#### Sample with equal probability #### Sample with equal probability
```javascript ```javascript
var myArray = [1,2,3,4,5]; var myArray = [1,2,3,4,5];
var sample = jsPsych.randomization.sampleWithReplacement(myArray, 10); var sample = jsPsych.randomization.sampleWithReplacement(myArray, 10);
// output: sample = [3, 1, 2, 2, 5, 1, 4, 3, 1, 5]; // output: sample = [3, 1, 2, 2, 5, 1, 4, 3, 1, 5];
``` ```
#### Sample with unequal probability #### Sample with unequal probability
```javascript ```javascript
var myArray = [1,2,3,4,5]; var myArray = [1,2,3,4,5];
var sample = jsPsych.randomization.sampleWithReplacement(myArray, 10, [6,1,1,1,1]); var sample = jsPsych.randomization.sampleWithReplacement(myArray, 10, [6,1,1,1,1]);
// output: sample = [3, 4, 5, 1, 2, 1, 3, 1, 1, 1]; // output: sample = [3, 4, 5, 1, 2, 1, 3, 1, 1, 1];
``` ```
--- ---
## jsPsych.randomization.sampleWithoutReplacement ## jsPsych.randomization.sampleWithoutReplacement
``` ```javascript
jsPsych.randomization.sampleWithoutReplacement(array, sampleSize) jsPsych.randomization.sampleWithoutReplacement(array, sampleSize)
``` ```
@ -303,17 +299,16 @@ This method returns a sample drawn at random from a set of values without replac
#### Sample without replacement #### Sample without replacement
```javascript ```javascript
var myArray = [1,2,3,4,5]; var myArray = [1,2,3,4,5];
var sample = jsPsych.randomization.sampleWithoutReplacement(myArray, 2); var sample = jsPsych.randomization.sampleWithoutReplacement(myArray, 2);
// output: sample = [3,2]; // output: sample = [3,2];
``` ```
--- ---
## jsPsych.randomization.shuffle ## jsPsych.randomization.shuffle
``` ```javascript
jsPsych.randomization.shuffle(array) jsPsych.randomization.shuffle(array)
``` ```
@ -336,17 +331,16 @@ A simple method for shuffling the order of an array.
#### Shuffle an array #### Shuffle an array
```javascript ```javascript
var myArray = [1,2,3,4,5]; var myArray = [1,2,3,4,5];
var shuffledArray = jsPsych.randomization.shuffle(myArray); var shuffledArray = jsPsych.randomization.shuffle(myArray);
// output: shuffledArray = [3,2,4,1,5] // output: shuffledArray = [3,2,4,1,5]
``` ```
--- ---
## jsPsych.randomization.shuffleNoRepeats ## jsPsych.randomization.shuffleNoRepeats
``` ```javascript
jsPsych.randomization.shuffleNoRepeats(array, equalityTest) jsPsych.randomization.shuffleNoRepeats(array, equalityTest)
``` ```
@ -372,10 +366,8 @@ Shuffle an array, ensuring that neighboring elements in the array are different.
#### Basic example #### Basic example
```javascript ```javascript
var myArray = [1,2,3,4,5,1,2,3,4,5,1,2,3,4,5]; var myArray = [1,2,3,4,5,1,2,3,4,5,1,2,3,4,5];
var shuffledArray = jsPsych.randomization.shuffleNoRepeats(myArray); var shuffledArray = jsPsych.randomization.shuffleNoRepeats(myArray);
// output: shuffledArray = [2, 3, 5, 1, 2, 4, 1, 5, 4, 1, 3, 5, 4, 3, 2] // output: shuffledArray = [2, 3, 5, 1, 2, 4, 1, 5, 4, 1, 3, 5, 4, 3, 2]
``` ```

View File

@ -3,9 +3,10 @@
The jsPsych.turk module contains functions for interacting with Mechanical Turk. The jsPsych.turk module contains functions for interacting with Mechanical Turk.
--- ---
## jsPsych.turk.submitToTurk ## jsPsych.turk.submitToTurk
``` ```javascript
jsPsych.turk.submitToTurk(data) jsPsych.turk.submitToTurk(data)
``` ```
@ -28,7 +29,6 @@ This method will only work when called from within the mechanical turk website.
### Example ### Example
```html ```html
<p>Enter the code you were given:</p> <p>Enter the code you were given:</p>
<input type="text" id="code"></input> <input type="text" id="code"></input>
<button onclick="sendData();">Submit HIT</button> <button onclick="sendData();">Submit HIT</button>
@ -52,7 +52,7 @@ function sendData() {
## jsPsych.turk.turkInfo ## jsPsych.turk.turkInfo
``` ```javascript
jsPsych.turk.turkInfo() jsPsych.turk.turkInfo()
``` ```
@ -78,7 +78,6 @@ This method returns basic information about the current Mechanical Turk session,
### Example ### Example
```javascript ```javascript
var turkInfo = jsPsych.turk.turkInfo(); var turkInfo = jsPsych.turk.turkInfo();
alert('Worker ID is: ' + turkInfo.workerId); alert('Worker ID is: ' + turkInfo.workerId);
@ -97,6 +96,3 @@ alert('Preview mode? ' + turkInfo.previewMode);
// false otherwise. // false otherwise.
alert('Outside turk? ' + turkInfo.outsideTurk); alert('Outside turk? ' + turkInfo.outsideTurk);
``` ```

View File

@ -8,7 +8,6 @@ jsPsych offers the ability to call arbitrary functions in response to certain ev
The `on_close` callback can be declared in the `jsPsych.init` method. The callback triggers when the user leaves the page, but before any content on the page is removed from the browser's memory. This can be used, for example, to save data as the user is leaving the page. The `on_close` callback can be declared in the `jsPsych.init` method. The callback triggers when the user leaves the page, but before any content on the page is removed from the browser's memory. This can be used, for example, to save data as the user is leaving the page.
```javascript ```javascript
jsPsych.init({ jsPsych.init({
timeline: exp, timeline: exp,
@ -25,7 +24,6 @@ jsPsych.init({
The `on_data_update` callback can be declared in the `jsPsych.init` method. The callback triggers at the end of a data update cycle. This happens after every trial, after the on_finish (trial) and on_trial_finish events execute, allowing you to modify the data in those callbacks, and then use this callback to store the data. The function will be passed a single argument, which contains the data that was written. The `on_data_update` callback can be declared in the `jsPsych.init` method. The callback triggers at the end of a data update cycle. This happens after every trial, after the on_finish (trial) and on_trial_finish events execute, allowing you to modify the data in those callbacks, and then use this callback to store the data. The function will be passed a single argument, which contains the data that was written.
```javascript ```javascript
jsPsych.init({ jsPsych.init({
timeline: exp, timeline: exp,
@ -34,6 +32,7 @@ jsPsych.init({
} }
}); });
``` ```
--- ---
## on_finish (trial) ## on_finish (trial)
@ -53,6 +52,7 @@ var trial = {
} }
}; };
``` ```
--- ---
## on_finish (experiment) ## on_finish (experiment)
@ -67,6 +67,7 @@ jsPsych.init({
} }
}); });
``` ```
--- ---
## on_load ## on_load
@ -82,6 +83,7 @@ var trial = {
} }
}; };
``` ```
--- ---
## on_start (trial) ## on_start (trial)
@ -123,6 +125,7 @@ var procedure = {
} }
} }
``` ```
--- ---
## on_timeline_start ## on_timeline_start
@ -141,6 +144,7 @@ var procedure = {
} }
} }
``` ```
--- ---
## on_trial_finish ## on_trial_finish
@ -156,13 +160,13 @@ jsPsych.init({
} }
}); });
``` ```
--- ---
## on_trial_start ## on_trial_start
The `on_trial_start` callback can be declared in the `jsPsych.init` method. The callback will trigger at the start of every trial in the experiment. The function receives a single argument: a modifiable copy of the trial object that will be used to run the next trial. Changes can be made to this object to alter the parameters of the upcoming trial. The `on_trial_start` callback can be declared in the `jsPsych.init` method. The callback will trigger at the start of every trial in the experiment. The function receives a single argument: a modifiable copy of the trial object that will be used to run the next trial. Changes can be made to this object to alter the parameters of the upcoming trial.
```javascript ```javascript
var current_score = 0; // a variable that is updated throughout the experiment to keep track of the current score. var current_score = 0; // a variable that is updated throughout the experiment to keep track of the current score.