Skip to main content

k6/timers

The k6/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
Returns: Timer ID that can be used with clearTimeout()

clearTimeout()

Cancels a timeout previously established by calling setTimeout().
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
Returns: Interval ID that can be used with clearInterval()

clearInterval()

Cancels a timed, repeating action previously established by calling setInterval().
number
required
Interval ID returned by setInterval()

Real-World Example

Based on the k6 source example showing timer behavior:

Important Notes

Timer functions in k6 are executed within the VU context. Unlike browser JavaScript, timers do not extend the VU iteration time beyond the default function execution.
These are the same timer functions available globally in browsers. The k6/timers module explicitly exports them for use in k6 scripts.
Use timers to simulate real-world scenarios like polling, periodic checks, or delayed actions within your load tests.

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