Go/No-Go task jsPsych 8.3.0
Build a simple response-inhibition task: press the spacebar for green circles (Go) and withhold the response for red circles (No-Go). Follow the sample code, run it, and inspect how each response is recorded.
Respond to Go stimuli and withhold responses to No-Go stimuli
Green circle: press the spacebar. Red circle: do not press.
- An instruction screen explains the rule. Press the spacebar to begin.
- A fixation cross (+) appears for 500 milliseconds.
- A green or red circle appears for 1,000 milliseconds.
- Repeat the fixation and stimulus steps for eight trials, then display and download the data.
The sample contains four Go and four No-Go trials. It shuffles their order, but a shuffle does not guarantee that the same condition will never occur several times in a row. The short, balanced sequence is only for learning how the code works.
Read the sample from top to bottom
-
Load jsPsych and the keyboard-response plugin
dist/jspsych.jsloads the jsPsych library.dist/plugin-html-keyboard-response.jsprovides the trial type used to show instructions, the fixation cross, and the colored circles while collecting keyboard responses.dist/jspsych.csssupplies the default display styles.These paths are relative to the sample HTML file. The sample must remain next to
dist/; do not move it into this lesson'sen/folder. -
Initialize the experiment and define its finish behavior
const jsPsych = initJsPsych({...})creates the jsPsych controller. Theon_finishcallback runs after the full timeline ends.displayData('csv')displays the recorded data in CSV format on the page.localSave('csv', 'gonogo-data.csv')asks the browser to download the CSV file to the participant's device; it does not send data to a server. -
Show the instructions
The
instructionobject usesjsPsychHtmlKeyboardResponse. Itsstimuluscontains the task rules, andchoices: [' ']means the spacebar advances from the instruction screen.The visible space between the quotation marks is intentional:
' 'represents the spacebar key. It is not the same as an empty string. -
Define the fixation trial
The
fixationobject displays a plus sign.choices: 'NO_KEYS'means no key can end the trial, andtrial_duration: 500ends it automatically after 500 milliseconds (half a second).This same fixation object is added before every colored-circle trial.
-
Make a list of conditions and shuffle their order
originalSequenceis an array containing four'go'values and four'nogo'values. An array is an ordered list; each value represents the condition for one stimulus trial.jsPsych.randomization.shuffle(originalSequence)returns the conditions in a randomized order. The counts stay at four of each, but the shuffle can still produce repeated Go or No-Go trials next to each other. -
Start the timeline with the instruction screen
const timeline = [instruction];creates the experiment's ordered trial list and places the instruction screen first. The following loop adds the fixation and stimulus trials after it. -
Use a loop to add each pair of trials
for (const kind of sequence)takes each condition from the shuffled array, one at a time. For every condition,timeline.push(fixation)adds the fixation trial, and the nexttimeline.push({...})adds its stimulus trial.This is why the code describes the fixation and stimulus once, then repeats the instructions for all eight conditions.
kindis the current value: either'go'or'nogo'. -
Choose the circle color from the condition
In the stimulus template,
kind === 'go' ? 'green' : 'red'is a conditional expression: ifkindis'go', use green; otherwise, use red. The circle is shown inside a<div>with a font size of 72 pixels.data: { condition: kind }stores the condition on that stimulus trial so the result can later be matched to the participant's response. -
Set the response window and keep its duration fixed
choices: [' ']accepts the spacebar.trial_duration: 1000sets the response window to one second.response_ends_trial: falsemeans that a keypress does not end the trial early; the circle remains on screen for the full second.If no key is pressed, the recorded
responseandrt(reaction time) are empty for that stimulus trial. If the key is pressed,rtis measured in milliseconds from the start of the stimulus. -
Calculate whether each response was correct
The
on_finishfunction runs when a stimulus trial ends. It assigns a Boolean value (trueorfalse) todata.correct.(kind === 'go')is true for Go trials.(data.response === ' ')is true when the spacebar was pressed. The two sides are compared: Go + press is correct, and No-Go + no press is correct. -
Run the timeline and inspect the CSV
jsPsych.run(timeline)starts the experiment. When it finishes, the data are displayed and a CSV download is offered.Use
conditionto identify Go and No-Go stimulus trials,responseto see whether a key was pressed,rtfor reaction time, andcorrectfor the trial-by-trial judgment. Instruction and fixation rows also appear in the data; exclude rows without a Go/No-Goconditionwhen summarizing the stimulus trials.
View the shared sample code
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Go/NoGo課題 / Go/No-Go task | jsPsych 8.3.0</title>
<script src="dist/jspsych.js"></script>
<script src="dist/plugin-html-keyboard-response.js"></script>
<link rel="stylesheet" href="dist/jspsych.css">
</head>
<body></body>
<script>
const jsPsych = initJsPsych({
on_finish: () => {
jsPsych.data.displayData('csv');
jsPsych.data.get().localSave('csv', 'gonogo-data.csv');
}
});
const instruction = {
type: jsPsychHtmlKeyboardResponse,
stimulus: '<p>緑色の丸が出たらスペースキーを押してください。<br>Press the spacebar when you see a green circle.</p>' +
'<p>赤色の丸では押さないでください。<br>Do not press for a red circle.</p>' +
'<p>スペースキーで開始します。<br>Press the spacebar to begin.</p>',
choices: [' ']
};
const fixation = {
type: jsPsychHtmlKeyboardResponse,
stimulus: '<div style="font-size:48px">+</div>',
choices: 'NO_KEYS',
trial_duration: 500
};
const originalSequence = [
'go', 'go', 'go', 'go',
'nogo', 'nogo', 'nogo', 'nogo'
];
const sequence = jsPsych.randomization.shuffle(originalSequence);
const timeline = [instruction];
for (const kind of sequence) {
timeline.push(fixation);
timeline.push({
type: jsPsychHtmlKeyboardResponse,
stimulus: `<div style="font-size:72px;color:${kind === 'go' ? 'green' : 'red'}">●</div>`,
choices: [' '],
trial_duration: 1000,
response_ends_trial: false,
data: { condition: kind },
on_finish: (data) => {
data.correct = (kind === 'go') === (data.response === ' ');
}
});
}
jsPsych.run(timeline);
</script>
</html>
