Function delayLoop

  • Async generator that loops at a given interval.

    Parameters

    • timeout: Interval

      Delay. If 0 is given, requestAnimationFrame is used over setTimeout.

    Returns AsyncGenerator<undefined, void, unknown>

    For Await loop every second

    const loop = delayLoop(1000);
    // Or: const loop = delayLoop({ secs: 1 });
    for await (const o of loop) {
    // Do something...
    // Warning: loops forever
    }

    Loop runs every second

    (async () => {
    const loop = delayLoop(1000);
    // or: loop = delayLoop({ secs: 1 });
    while (true) {
    await loop.next();

    // Do something...
    // Warning: loops forever
    }
    })();

    Alternatives:

    • delay to run a single function after a delay
    • sleep pause execution
    • continuously to start/stop/adjust a constantly running loop