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.
callback
function
required
Function to execute after the delay
delay
number
required
Delay in milliseconds before executing the callback
...args
any
Optional arguments to pass to the callback function
Returns: Timer ID that can be used with clearTimeout()
import { setTimeout } from "k6/timers";

export default function () {
  console.log("Starting test");
  
  setTimeout(() => {
    console.log("Executed after 2 seconds");
  }, 2000);
  
  setTimeout((msg) => {
    console.log(msg);
  }, 1000, "Hello from timer");
}

clearTimeout()

Cancels a timeout previously established by calling setTimeout().
timeoutId
number
required
Timer ID returned by setTimeout()
import { setTimeout, clearTimeout } from "k6/timers";

export default function () {
  const timeoutId = setTimeout(() => {
    console.log("This will not execute");
  }, 5000);
  
  // Cancel the timeout
  clearTimeout(timeoutId);
}

setInterval()

Repeatedly calls a function with a fixed time delay between each call.
callback
function
required
Function to execute repeatedly
delay
number
required
Delay in milliseconds between each execution
...args
any
Optional arguments to pass to the callback function
Returns: Interval ID that can be used with clearInterval()
import { setInterval, clearInterval } from "k6/timers";
import { sleep } from "k6";

export default function () {
  let count = 0;
  
  const intervalId = setInterval(() => {
    count++;
    console.log(`Interval execution #${count}`);
    
    if (count >= 5) {
      clearInterval(intervalId);
    }
  }, 1000);
  
  sleep(6); // Keep VU alive to see interval executions
}

clearInterval()

Cancels a timed, repeating action previously established by calling setInterval().
intervalId
number
required
Interval ID returned by setInterval()
import { setInterval, clearInterval } from "k6/timers";

export default function () {
  const intervalId = setInterval(() => {
    console.log("This executes every second");
  }, 1000);
  
  // Stop after 5 seconds
  setTimeout(() => {
    clearInterval(intervalId);
    console.log("Interval stopped");
  }, 5000);
}

Real-World Example

Based on the k6 source example showing timer behavior:
import { setTimeout } from "k6/timers";

let last = 0;
let iterations = 10;

function timeout() {
  // Log the time of this call
  logline(new Date().getMilliseconds());
  
  // If we are not finished, schedule the next call
  if (iterations-- > 0) {
    setTimeout(timeout, 0);
  }
}

export default function () {
  // Initialize iteration count and the starting timestamp
  iterations = 10;
  last = new Date().getMilliseconds();
  
  // Start timer
  setTimeout(timeout, 0);
}

function pad(number) {
  return number.toString().padStart(3, "0");
}

function logline(now) {
  // Log the last timestamp, the new timestamp, and the difference
  console.log(`${pad(last)}         ${pad(now)}          ${now - last}`);
  last = now;
}

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

Build docs developers (and LLMs) love