launchPeriodic

fun CoroutineScope.launchPeriodic(interval: Duration, initialDelay: Duration = Duration.ZERO, action: suspend () -> Unit): Job

Launches a coroutine that runs action repeatedly with a fixed pause between runs.

The first run starts after initialDelay; each following run starts interval after the previous run finished. A failure thrown by action stops the loop and propagates through the returned Job to the scope.

Cancellation: cancelling the returned Job, or the scope, stops the loop at the next suspension point.

Parameters

interval

the pause between the end of one run and the start of the next.

initialDelay

the pause before the first run.

Throws

if interval is not positive or initialDelay is negative.