commonMain.ru.casperix.misc.time.ManualTimer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of misc Show documentation
Show all versions of misc Show documentation
Small things that did not find a place in respected libraries
package ru.casperix.misc.time
/**
* You call update in loop.
*
* TImer calculate time to call onEvent
*
* @param delay -- in milliseconds before first call
* @param interval -- in milliseconds between calls
*/
class ManualTimer(val delay: Double, val interval: Double, val maxUpdateTimeMs:Int = 100, val onEvent: () -> Unit) {
private var accumulatedTime = interval - delay
fun update(time: Double) {
accumulatedTime += time
val startTimeMs = getTimeMs()
while (accumulatedTime >= interval) {
accumulatedTime -= interval
onEvent()
val nextTimeMs = getTimeMs()
if (startTimeMs + maxUpdateTimeMs < nextTimeMs) {
//overtime
accumulatedTime = 0.0
return
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy