Overview
Thedelay operator time-shifts each item emitted by the source Observable by a specified amount of time (in milliseconds) or until a specific Date occurs. The relative time intervals between values are preserved.
Signature
Parameters
The delay duration in milliseconds (as a
number) or a Date until which the emission of the source items is delayed.- number: Delays each emission by this many milliseconds
- Date: Delays the start of emissions until this date/time
The scheduler to use for managing the timers that handle the time-shift for each item.
Returns
A function that returns an Observable that delays the emissions of the source Observable by the specified timeout or Date.
Usage Examples
Delay by Milliseconds
Delay each click event by 1 second:Delay Until Date
Delay all emissions until a future date:Staggered Animation
Create a staggered animation effect:Debounce Effect
Combine with other operators for complex timing:How It Works
delay preserves the relative timing between emissions. If source emits at t=0ms, t=100ms, t=200ms, and you apply delay(1000), the result emits at t=1000ms, t=1100ms, t=1200ms.- Each value from the source is queued
- A timer is started for that value
- After the delay period, the value is emitted
- Relative spacing between values is maintained
- The operator waits until that date/time
- Then all subsequent values are emitted normally
- If the date is in the past, emissions start immediately
Common Use Cases
- Simulating Network Latency: Test how your app handles delayed responses
- Animation Timing: Create sequential animations with precise timing
- Rate Limiting: Add delays between API calls
- User Feedback: Delay showing loading indicators to avoid flashing
- Scheduled Actions: Start processing at a specific time
Comparison with Similar Operators
| Operator | Delay Type | Use Case |
|---|---|---|
delay | Fixed duration or date | Consistent delays for all items |
delayWhen | Dynamic per value | Variable delays based on value |
debounceTime | Resets on each emission | Wait for pause in emissions |
throttleTime | Limits rate | Emit at most once per time period |
Implementation Details
delay is implemented using delayWhen internally:
Performance Considerations
- Each delayed value creates a timer/subscription
- For many rapid emissions, consider using
bufferTimeorauditTimeinstead - The scheduler parameter allows optimization for different contexts (browser animations, testing, etc.)
Related Operators
- delayWhen - Delay based on another Observable
- debounceTime - Delay and discard rapid emissions
- throttleTime - Emit at most once per time period
- timeout - Error if emissions are too slow
- timer - Create Observable that emits after delay
