# class Timer

#### package haxe

Available on all platforms

The Timer class allows you to create asynchronous timers on platforms that support events.

The intended usage is to create an instance of the Timer class with a given interval, set its run() method to a custom function to be invoked and eventually call stop() to stop the Timer.

Note that a running Timer may or may not prevent the program to exit automatically when main() returns.

It is also possible to extend this class and override its run() method in the child class.

### Constructor

###

`new (time_ms:Int)`

Available on Flash, HTML5

Creates a new timer that will run every `time_ms` milliseconds.

After creating the Timer instance, it calls `this.run` repeatedly, with delays of `time_ms` milliseconds, until `this.stop` is called.

The first invocation occurs after `time_ms` milliseconds, not immediately.

The accuracy of this may be platform-dependent.

###

`new (time:Float)`

Available on Android, iOS, Linux, macOS, Neko, Windows

Creates a new timer that will run every `time_ms` milliseconds.

After creating the Timer instance, it calls `this.run` repeatedly, with delays of `time_ms` milliseconds, until `this.stop` is called.

The first invocation occurs after `time_ms` milliseconds, not immediately.

The accuracy of this may be platform-dependent.

### Methods

###

`dynamic run ():Void`

This method is invoked repeatedly on `this` Timer.

It can be overridden in a subclass, or rebound directly to a custom function:

    var timer = new haxe.Timer(1000); // 1000ms delay
    timer.run = function() { ... }

Once bound, it can still be rebound to different functions until `this` Timer is stopped through a call to `this.stop`.

###

`stop ():Void`

Stops `this` Timer.

After calling this method, no additional invocations of `this.run` will occur.

It is not possible to restart `this` Timer once stopped.

### Static methods

###

`staticdelay (f:Void ‑> Void, time:Int):Timer`

Available on Android, iOS, Linux, macOS, Neko, Windows

Invokes `f` after `time_ms` milliseconds.

This is a convenience function for creating a new Timer instance with `time_ms` as argument, binding its run() method to `f` and then stopping `this` Timer upon the first invocation.

If `f` is null, the result is unspecified.

###

`staticdelay (f:Void ‑> Void, time_ms:Int):Timer`

Available on Flash, HTML5

Invokes `f` after `time_ms` milliseconds.

This is a convenience function for creating a new Timer instance with `time_ms` as argument, binding its run() method to `f` and then stopping `this` Timer upon the first invocation.

If `f` is null, the result is unspecified.

###

`staticmeasure<T> (f:Void ‑> T, ?pos:PosInfos):T`

Measures the time it takes to execute `f`, in seconds with fractions.

This is a convenience function for calculating the difference between Timer.stamp() before and after the invocation of `f`.

The difference is passed as argument to Log.trace(), with "s" appended to denote the unit. The optional `pos` argument is passed through.

If `f` is null, the result is unspecified.

###

`staticinline stamp ():Float`

Returns a timestamp, in seconds with fractions.

The value itself might differ depending on platforms, only differences between two values make sense.