All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.ru.casperix.misc.time.ManualTimer.kt Maven / Gradle / Ivy

There is a newer version: 1.8.4
Show newest version
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