📜  kotlin 每秒运行任务 - Kotlin 代码示例

📅  最后修改于: 2022-03-11 14:53:34.604000             🧑  作者: Mango

代码示例1
val scope = MainScope() // could also use an other scope such as viewModelScope if available
var job: Job? = null

fun startUpdates() {
    stopUpdates()
    job = scope.launch {
        while(true) {
            getData() // the function that should be ran every second
            delay(1000)
        }
    }
}

fun stopUpdates() {
    job?.cancel()
    job = null
}