cancelInterval
Call this method within an interval handler to prevent the interval from running any additional times.
For example, here's how you would create a simple composable which types out text, stopping the interval automatically after the text has finished being typed out:
@Composable
fun TypedText(
value: String,
modifier: Modifier = Modifier,
keystrokeDelay: Duration = 50.milliseconds
) {
var currText by remember { mutableStateOf("") }
SpanText(currText, modifier, ref = ref {
window.setInterval(keystrokeDelay) {
if (currText.length < value.length) {
currText += value[currText.length]
} else cancelInterval()
}
})
}
Content copied to clipboard