TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/grafana/k6-docs/llms.txt
Use this file to discover all available pages before exploring further.
k6/timers module implements timer functions that work with k6’s event loop. These functions mimic the functionality found in browsers and other JavaScript runtimes.
Overview
Use timer functions to schedule code execution at specific times or intervals within your k6 tests. These are particularly useful for:- Simulating user think time with delays
- Creating periodic actions (polling, heartbeats)
- Implementing timeouts for WebSocket or browser tests
- Controlling test flow and timing
Timer functions are available globally in k6 scripts. You don’t need to import them unless you prefer explicit imports.
Importing the Module
API Reference
setTimeout
Schedules a function to run after a specified delay.Parameters:Returns:
Function to execute after the delay
Delay in milliseconds before executing the callback
Optional arguments to pass to the callback function
Timeout identifier that can be used with
clearTimeout()clearTimeout
Cancels a timeout previously established by calling
setTimeout().Parameters:The timeout identifier returned by
setTimeout()setInterval
Schedules a function to run repeatedly at specified intervals.Parameters:Returns:
Function to execute at each interval
Time in milliseconds between each execution
Optional arguments to pass to the callback function
Interval identifier that can be used with
clearInterval()clearInterval
Cancels an interval previously established by calling
setInterval().Parameters:The interval identifier returned by
setInterval()Examples
Basic Timeout Example
Basic Interval Example
WebSocket Heartbeat
Browser Test with Timeout
Simulating User Think Time
Polling Pattern
Cancelling Timers
Best Practices
Always Clear Timers
Always Clear Timers
Always clear intervals and timeouts when they’re no longer needed to prevent memory leaks and unexpected behavior.
Use sleep() for Simple Delays
Use sleep() for Simple Delays
For simple delays in the default function, use
sleep() from k6 instead of setTimeout(). It’s more straightforward and doesn’t require event loop handling.Event Loop Requirement
Event Loop Requirement
Timers require an active event loop. They work best in:
- WebSocket tests (using
k6/websockets) - Browser tests (using
k6/browser) - Scenarios with long-running connections
Precision Considerations
Precision Considerations
Timer precision is not guaranteed. Actual execution may be delayed if:
- The event loop is busy
- System resources are limited
- Other operations are blocking
Timers vs sleep()
| Feature | Timers (setTimeout/setInterval) | sleep() |
|---|---|---|
| Execution | Asynchronous (event loop) | Synchronous (blocks) |
| Use Case | WebSocket, browser, async ops | Simple delays in HTTP tests |
| Cancellable | Yes (clearTimeout/clearInterval) | No |
| Multiple Actions | Yes (multiple timers) | No (one delay at a time) |
| Event Loop Required | Yes | No |
Related Resources
sleep()
Simple blocking delay function
k6/websockets
WebSocket API that works with timers
k6/browser
Browser API that uses timers
MDN: Timers
Browser timer API documentation