Variable interval operation

I’d like to be able set an interval with a variable time for each invocation. I tried the following, but it doesn’t seem to work s expected. When invoked I see the following in the logs. There is no delay before the next callback function is invoked.

time="2022-04-26T15:49:48-04:00" level=info msg="[2022-04-26T19:49:48.989Z] (vu1) WS connected" source=console
time="2022-04-26T15:49:51-04:00" level=info msg="[2022-04-26T19:49:51.319Z] (vu1) sendOrder: next interval 3000.24" source=console
time="2022-04-26T15:49:51-04:00" level=info msg="[2022-04-26T19:49:51.319Z] (vu1) sendOrder: next interval 3000.118" source=console
time="2022-04-26T15:49:51-04:00" level=info msg="[2022-04-26T19:49:51.319Z] (vu1) sendOrder: next interval 2999.825" source=console
time="2022-04-26T15:49:51-04:00" level=info msg="[2022-04-26T19:49:51.319Z] (vu1) sendOrder: next interval 2999.765" source=console
....
    res = ws.connect(WS_URL, PARAMS, (socket) => {

      function sendOrder() {
        const orderInterval =
          TIMERS.orderInterval +
          randomInteger(-TIMERS.orderInterval / 10, TIMERS.orderInterval / 10) /
            1000;
        log(`sendOrder: next interval ${orderInterval}`);
        socket.setTimeout(sendOrder(), orderInterval);
      }
     ...
      socket.on("open", () => {
         log("WS connected");
         sendOrder();
      });
....

This should be socket.setTimeout(sendOrder, orderInterval);, right now you are recursively invoking the function before you even call setTimeout, while the point is to have k6 call it on its own after the given timeout has passed.