菜单
开源 RSS

k6/timers

k6/timers 模块实现了与 k6 事件循环配合使用的定时器。它们模仿了浏览器和其他 JavaScript 运行时中的功能。

函数描述
setTimeout设置一个函数在给定超时时间后运行。
clearTimeout清除之前使用 setTimeout 设置的超时。
setInterval设置一个函数按给定间隔运行。
clearInterval清除之前使用 setInterval 设置的间隔。

注意

定时器方法是全局可用的,因此您可以在脚本中直接使用它们,无需包含 import 语句。

示例

JavaScript
export default function () {
  const intervalId = setInterval(() => {
    console.log('This runs every 200ms');
  }, 200);

  const timeoutId = setTimeout(() => {
    console.log('This runs after 2s');

    // clear the timeout and interval to exit k6
    clearInterval(intervalId);
    clearTimeout(timeoutId);
  }, 2000);
}