KTimer

class KTimer(scope: CoroutineScope, val delay: Duration?, block: suspend () -> Unit)(source)

A cancellable one-shot timer that fires block after delay.

Port of the useTimeout React hook.

val timer = KTimer(scope = viewModelScope, delay = 2.seconds) {
println("fired!")
}
// Runs automatically. To restart:
timer.reset()
// To stop:
timer.clear()

Parameters

scope

CoroutineScope that owns the timer job.

delay

How long to wait before firing block. Pass null to start paused.

block

Suspending action called once when the timer fires.

Constructors

Link copied to clipboard
constructor(scope: CoroutineScope, delay: Duration?, block: suspend () -> Unit)

Properties

Link copied to clipboard

Functions

Link copied to clipboard
fun clear()

Cancels the pending timer without firing block.

Link copied to clipboard
fun reset()

Cancels the current timer and starts a fresh one.

Link copied to clipboard
fun set()

Starts (or restarts) the timer. No-op if delay is null.