k6/timers
Thek6/timers module provides standard JavaScript timer functions for scheduling asynchronous operations within k6 tests.
Functions
setTimeout()
Schedules a function to be executed after a specified delay.function
required
Function to execute after the delay
number
required
Delay in milliseconds before executing the callback
any
Optional arguments to pass to the callback function
clearTimeout()
clearTimeout()
Cancels a timeout previously established by callingsetTimeout().
number
required
Timer ID returned by setTimeout()
setInterval()
Repeatedly calls a function with a fixed time delay between each call.function
required
Function to execute repeatedly
number
required
Delay in milliseconds between each execution
any
Optional arguments to pass to the callback function
clearInterval()
clearInterval()
Cancels a timed, repeating action previously established by callingsetInterval().
number
required
Interval ID returned by setInterval()
Real-World Example
Based on the k6 source example showing timer behavior:Important Notes
These are the same timer functions available globally in browsers. The
k6/timers module explicitly exports them for use in k6 scripts.Use Cases
- Simulating user behavior: Add realistic delays between actions
- Polling endpoints: Check status at regular intervals
- Cleanup operations: Schedule cleanup after test actions
- Asynchronous workflows: Coordinate multiple async operations