What do you think about this implementation?

inline fun every(duration: Long, timeUnit: TimeUnit, whileCondition: () -> Boolean = { true }, function: () -> Unit) {
    while (whileCondition()) {

This allows calling the following statement:

every(5, TimeUnit.SECONDS){
    println("Hello world")

Usually it's a bad idea to put to sleep whole thread. I'd recommend use Java Timer or kotlin's wrapper for it.

Here is an example:

fixedRateTimer("default", false, 0L, 1000){
    if (theEndIsNear) cancel()
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.