View source

class thx.Timer

Available on java, js, swf

Timer provides several meaning to delay the execution of code. At the moment it is only implemented for platforms that have a native concept of Timer like Swf and JavaScript or c++/Neko with OpenFL or NME.

All of the Timer methods return a function with signature Void -> Void that can be used to cancel the timer.

// set the execution delayed by 200ms
var cancel = Timer.delay(doSomethingLater, 200);

// cancel immediately (doSomethingLater will never be executed)

Note that calling the cancel function multiple times have no effect after the first execution.

Class Fields

static function debounce(callback:Void -> Void, delayms:Int, leading:Bool = false):Void -> Void

Creates a function that delays the execution of callback by delayms every time it is invoked. If leading is set to true, a first execution is guaranteed to happen as soon as the returnd function is invoked.

static function delay(callback:Void -> Void, delayms:Int):Void -> Void

Timer.delay invokes callback after delayms milliseconds. The scheduling can be canelled using the returned cancel function.

static function frame(callback:Float -> Void):Void -> Void

Invokes callback at every frame using native implementation where available. A delta time in milliseconds is passed since the latest time callback was invoked.

static function immediate(callback:Void -> Void):Void -> Void

Timer.immediate works essentially like Timer.delay with the exception that the delay will be the shortest allowed by the platform. How short the delay depends a lot on the target platform.

static function nextFrame(callback:Void -> Void):Void -> Void

Delays callback untile the next frame using native implementation where available.

static function repeat(callback:Void -> Void, delayms:Int):Void -> Void

Timer.repeat continues to invoke callback until it is cancelled using the returned cancel function.

static function resolution():Float

static function throttle(callback:Void -> Void, delayms:Int, leading:Bool = false):Void -> Void

The returned function executes callback at most once every delayms regardless of how many times it is invoked in that timespance. Setting leading to true ensures that the callback is invoked at the beginning of the cycle.

static inline function time():Float

Returns a time value in milliseconds. Where supported, the decimal value represents microseconds.

Note that the initial value might change from platform to platform so only delta measurements make sense.

Toggle inherited fields