throttleTime

fun <T> Flow<T>.throttleTime(duration: Duration, throttleConfiguration: ThrottleConfiguration = LEADING): Flow<T>(source)

Returns a Flow that emits a value from the source Flow, then ignores subsequent source values for a duration, then repeats this process for the next source value.

(1..10)
.asFlow()
.onEach { delay(200) }
.throttleTime(500.milliseconds)

produces the following emissions

1, 4, 7, 10
(1..10)
.asFlow()
.onEach { delay(200) }
.throttleTime(500.milliseconds, TRAILING)

produces the following emissions

3, 6, 9, 10
(1..10)
.asFlow()
.onEach { delay(200) }
.throttleTime(500.milliseconds, LEADING_AND_TRAILING)

produces the following emissions

1, 3, 4, 6, 7, 9, 10

fun <T> Flow<T>.throttleTime(timeMillis: Long, throttleConfiguration: ThrottleConfiguration = LEADING): Flow<T>(source)

Returns a Flow that emits a value from the source Flow, then ignores subsequent source values for timeMillis milliseconds, then repeats this process for the next source value.

(1..10)
.asFlow()
.onEach { delay(200) }
.throttleTime(500)

produces the following emissions

1, 4, 7, 10
(1..10)
.asFlow()
.onEach { delay(200) }
.throttleTime(500, TRAILING)

produces the following emissions

3, 6, 9, 10
(1..10)
.asFlow()
.onEach { delay(200) }
.throttleTime(500, LEADING_AND_TRAILING)

produces the following emissions

1, 3, 4, 6, 7, 9, 10

fun <T> Flow<T>.throttleTime(throttleConfiguration: ThrottleConfiguration = LEADING, durationSelector: (value: T) -> Duration): Flow<T>(source)

Returns a Flow that emits a value from the source Flow, then ignores subsequent source values for a duration determined by durationSelector, then repeats this process for the next source value.

(1..10)
.asFlow()
.onEach { delay(200) }
.throttleTime { 500.milliseconds }

produces the following emissions

1, 4, 7, 10
(1..10)
.asFlow()
.onEach { delay(200) }
.throttleTime(TRAILING) { 500.milliseconds }

produces the following emissions

3, 6, 9, 10
(1..10)
.asFlow()
.onEach { delay(200) }
.throttleTime(LEADING_AND_TRAILING) { 500.milliseconds }

produces the following emissions

1, 3, 4, 6, 7, 9, 10