https://kotlinlang.org logo
#android
Title
# android
k

KotlinLeaner

03/15/2023, 11:26 AM
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

MarinJuricev

03/15/2023, 12:03 PM
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

gildor

03/15/2023, 12:17 PM
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

KotlinLeaner

03/15/2023, 2:57 PM
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

gildor

03/15/2023, 4:30 PM
Error on the screenshot is from duration constructor, not from delay
👍 1
k

KotlinLeaner

03/15/2023, 4:31 PM
ohh nice thanks, It works
👌 1
8 Views