What is a Timeline?
In Cajal (and jsPsych), a timeline is the sequence of events that make up your experiment. Think of it as the blueprint that defines what participants see, when they see it, and in what order.The timeline controls the flow of your experiment from start to finish. It specifies not just individual trials, but also how trials repeat, randomize, and connect together.
Timeline vs. Frames
- Frame: A single screen or moment in time (e.g., a fixation cross, a stimulus display)
- Timeline: The complete sequence of frames that make up your task
Frame-Based Timeline Structure
Understanding Frames
Frames are the building blocks of your timeline. Each frame represents a discrete moment in the experiment.Single Frame
One screen with components (e.g., “Show fixation cross”)
Frame Sequence
Multiple frames in order (e.g., “Fixation → Stimulus → Feedback”)
Frame Duration
How long each frame displays (fixed time or until response)
Frame Transitions
Auto-advance or wait for response
Frame Duration Options
Each frame can display for:- Fixed Duration: Exact time in milliseconds (e.g., 500ms)
- Until Response: Wait for participant input (0ms = unlimited)
- Maximum Duration: Display up to X ms, but advance on response
- Conditional Duration: Based on previous response or trial variable
- Fixed Duration Example
- Response-Based Example
- Hybrid Example
Fixation cross for exactly 500msThis frame will display for exactly 500ms, then automatically move to the next frame.
Timeline Variables
Timeline variables are the secret to running multiple trials with different stimuli while using the same trial structure.What Are Timeline Variables?
Timeline variables define per-trial data that changes across repetitions. Instead of creating 50 separate trials, you create one trial template and one table of variables.Think of timeline variables as a spreadsheet where each row is one trial, and each column is a variable that changes per trial (stimulus word, correct answer, condition, etc.).
Example: Stroop Task Timeline Variables
How Timeline Variables Work
- Define the table: Create columns for each variable that changes per trial
- Reference variables: Use
{{variable_name}}in component properties - Set repetition: Specify how many rows to use (all, subset, or specific count)
- Randomize: Optionally randomize row order
Trial Sequences and Loops
Basic Looping
The simplest timeline loops through all rows of your timeline variables exactly once. Example: 8 rows in timeline variables = 8 trialsBasic Loop Configuration
Settings:
- Repetitions: 1 (each row once)
- Randomize: Yes
- Sampling: Without replacement (each row used once)
Advanced Looping Options
Multiple Repetitions
Multiple Repetitions
Use case: Repeat each condition multiple timesConfiguration:
- Timeline Variables: 8 rows
- Repetitions: 3
- Result: 24 trials (each row appears 3 times)
Partial Sampling
Partial Sampling
Use case: Randomly sample subset of trialsConfiguration:
- Timeline Variables: 100 rows
- Sample Size: 20 trials
- Sampling: Random without replacement
- Result: 20 trials randomly selected from 100 options
Sampling with Replacement
Sampling with Replacement
Use case: Allow same trial to appear multiple timesConfiguration:
- Timeline Variables: 10 rows
- Sample Size: 30 trials
- Sampling: Random with replacement
- Result: 30 trials, some rows may repeat
Blocked Design
Blocked Design
Use case: Group trials into distinct blocksConfiguration:
- Block 1: Congruent trials only (filter
congruent == true) - Block 2: Incongruent trials only (filter
congruent == false) - Order: Randomize within blocks, counterbalance block order
Randomization and Counterbalancing
Randomization Options
Cajal supports multiple randomization strategies:- Full Randomization
- Pseudorandomization
- Fixed Order
- Block Randomization
Strategy: Completely random trial orderConfiguration:
- Randomize: Yes
- Constraints: None
Counterbalancing
Counterbalancing ensures condition order varies systematically across participants.Simple Counterbalancing
Simple Counterbalancing
Method: Alternate condition order based on participant IDExample:
- Participant 1, 3, 5…: Condition A → Condition B
- Participant 2, 4, 6…: Condition B → Condition A
Latin Square Counterbalancing
Latin Square Counterbalancing
Method: Systematically rotate condition order across participantsExample (3 conditions):
- Participant 1: A → B → C
- Participant 2: B → C → A
- Participant 3: C → A → B
Response Key Counterbalancing
Response Key Counterbalancing
Method: Alternate which key maps to which responseExample:
- Group 1: Z = Left, M = Right
- Group 2: M = Left, Z = Right
Sequential vs. Conditional vs. Randomized Flow
Sequential Flow
The simplest timeline: frames advance in a fixed order. Use case: Standard trial structure where every trial follows the same sequenceConditional Flow
Timeline branches based on participant responses or trial variables. Use case: Adaptive tasks, branching experiments, conditional feedback- Accuracy-Based Branching
- Response-Based Branching
- Performance-Based Branching
Logic: Show different feedback based on correctness
Randomized Flow
Trial order determined randomly at runtime. Use case: Prevent order effects, increase generalizabilityRandomization Levels
Within-Trial: Randomize component positions (e.g., left/right stimulus placement)Between-Trial: Randomize trial order from timeline variablesBetween-Block: Randomize block order (counterbalanced across participants)
Timeline Best Practices
Use Timeline Variables for Repetition
Don’t: Create 50 separate trial frames manuallyDo: Create 1 trial template + 50-row timeline variable tableThis reduces errors and makes updates easier.
Balance Your Conditions
Ensure equal trials per condition in your timeline variables.Example: 24 congruent trials, 24 incongruent trials (not 30 vs 18)
Include Practice Trials
Add a separate practice timeline before main trials:
- Fewer trials (4-8 typically)
- Same structure as main trials
- Show feedback (even if hidden in main trials)
- Allow participants to repeat if needed
Use Appropriate ISIs
Inter-stimulus interval (ISI) prevents visual persistence and resets attention.Typical ISIs:
- 500ms: Standard cognitive tasks
- 1000ms: After complex stimuli or feedback
- 2000ms: Between blocks or conditions
- Variable (500-1500ms): Prevent anticipatory responses
Test Timing Accuracy
Browser-based timing is accurate to ~10-20ms, sufficient for most psychology tasks.Tips for timing precision:
- Preload all images before trials start
- Use simple stimuli for timing-critical tasks
- Test on target hardware (desktop vs mobile)
- Avoid video backgrounds or animations during critical timing
Consider Mobile Compatibility
If participants may use mobile devices:
- Use button responses instead of keyboard
- Increase touch target sizes (min 44x44px)
- Test on smaller screens
- Avoid complex layouts that may not fit
Common Timeline Patterns
Pattern 1: Simple Repeated Trials
Use case: Basic cognitive task with identical trial structurePattern 2: Practice → Test Blocks
Use case: Training phase before main experimentPattern 3: Study → Delay → Test
Use case: Memory experiments with encoding and retrieval phasesPattern 4: Adaptive Difficulty
Use case: Adjust task difficulty based on performanceTimeline Variables Reference
Common Variable Types
| Variable Type | Example | Purpose |
|---|---|---|
| Stimulus Content | word, image_url, number | What to display |
| Correct Answer | correct_key, correct_button | Scoring responses |
| Condition Tags | congruent, condition, block | Experimental design |
| Timing | stimulus_duration, isi_duration | Variable timing |
| Position | stimulus_x, stimulus_y | Spatial control |
| Metadata | trial_type, difficulty_level | Data analysis |
Variable Naming Best Practices
- Good Variable Names
- Avoid These Names
Advanced Timeline Features
Nested Timelines
Create timelines within timelines for complex experimental structures. Example: A multi-block experimentTimeline Functions
Use JavaScript functions to dynamically generate timeline properties.- Dynamic Stimulus Selection
- Variable ITI
- Conditional Display
Timeline Looping with Conditions
Loop through trials until a condition is met (instead of fixed count). Example: Continue trials until participant reaches 80% accuracyData Collection from Timelines
Automatic Timeline Data
Every trial automatically records:| Data Field | Description |
|---|---|
trial_index | Sequential trial number (0-indexed) |
time_elapsed | Milliseconds since experiment start |
trial_type | jsPsych plugin used |
internal_node_id | Timeline position identifier |
Custom Timeline Data
Add custom data fields to each trial:Timeline-Level Data
Add data that applies to all trials in a timeline:Performance Considerations
Optimizing Timeline Performance
Preload Media
Load all images, audio, and video before trials start.Why: Prevents delays during trial presentationHow: Cajal automatically preloads media referenced in timeline variables
Minimize Timeline Variable Rows
Use repetition settings instead of duplicating rows.Don’t: 1000 rows with many duplicatesDo: 50 unique rows × 20 repetitions
Avoid Complex Calculations Per Trial
Pre-compute values in timeline variables when possible.Don’t: Calculate stimulus position on every trialDo: Pre-define positions in timeline variable columns
Test on Target Devices
Performance varies significantly between desktop and mobile.Desktop: Faster, more consistent timingMobile: Slower, more variable (test your actual design)
Troubleshooting Common Timeline Issues
Trials Not Randomizing
Trials Not Randomizing
Problem: Trials appear in same order every timeSolution:
- Check “Randomize” setting is enabled
- Ensure sampling method is set correctly
- Verify timeline variables table exists
Timeline Variables Not Displaying
Timeline Variables Not Displaying
Problem:
{{variable_name}} appears literally instead of valueSolution:- Check variable name spelling matches exactly
- Ensure timeline variables table has this column
- Verify variable is wrapped in double curly braces:
{{name}}
Wrong Number of Trials
Wrong Number of Trials
Problem: More or fewer trials than expectedSolution:
- Check repetition setting (rows × repetitions = total trials)
- Verify sampling method (with/without replacement)
- Look for nested timeline repetitions multiplying count
Timing Feels Off
Timing Feels Off
Problem: Stimuli display for wrong durationSolution:
- Verify duration is in milliseconds (not seconds)
- Check for response-based advancement overriding duration
- Ensure preloading is complete before trials start
- Test on target hardware (timing varies by device)
Conditions Unbalanced
Conditions Unbalanced
Problem: Some conditions appear more than othersSolution:
- Count rows per condition in timeline variables
- Ensure equal representation in source table
- Check sampling method isn’t creating imbalance

