Hi guys, I want to use `delay` in my launch functi...
# android
k
Hi guys, I want to use
delay
in my launch function. I always use
xxxL
value for
delay
. Is there any function to convert
seconds
,
minutes
&
hours
. For example / For 30 second
Copy code
delay(30)
// For 1 minute
Copy code
delay(1)
// For 2 Hour
Copy code
delay(2)
It's so much confusing to multiple by value. Thanks
m
You could possibly use something like this:
Copy code
delay(TimeUnit.SECONDS.toMillis(2))
TimeUnit exposes other enum values, such as:
MINUTES
,
HOURS
,
DAYS
, etc….
g
Delay supports Duration: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/delay.html (See overload which works with Duration, not with long)
👍 1
So you can do delay(1.minutes)
k
I am trying to use like this
Copy code
delay(Duration(1.minutes))
and imports are
Copy code
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.time.Duration
import kotlin.time.Duration.Companion.minutes
but I am getting
g
Error on the screenshot is from duration constructor, not from delay
👍 1
k
ohh nice thanks, It works
👌 1