Difference between revisions of "Oolite JavaScript Reference: Timer"

From Elite Wiki
m (nextTime: Renamed time to clock. Oh, and implemented it.)
m (nextTime: clock.absoluteSeconds is a property, not a method.)
Line 27: Line 27:
 
=== nextTime ===
 
=== nextTime ===
 
'''nextTime''' : Number (read/write when stopped)
 
'''nextTime''' : Number (read/write when stopped)
The next time the timer will fire, if it is running at that time. This can be modified if the timer is stopped. Note that this is an absolute time, not a delay. You can get the current absolute time using <code>[[Oolite/Development/Scripting/Class/Clock#absoluteSeconds|clock.absoluteSeconds()]]</code>.
+
The next time the timer will fire, if it is running at that time. This can be modified if the timer is stopped. Note that this is an absolute time, not a delay. You can get the current absolute time using <code>[[Oolite/Development/Scripting/Class/Clock#absoluteSeconds|clock.absoluteSeconds]]</code>.
   
 
== Methods ==
 
== Methods ==

Revision as of 16:29, 15 September 2007

Prototype: Object
Subtypes: none

Timer objects are used to perform tasks after a delay or on a repeating schedule. When a timer is created, it is given a function to call, and a delay and an optional interval in seconds in game real time. The next fire time of the timer is set to the current time + the delay. When the game clock reaches (or passes) the next fire time, the function is called. If the timer has a positive interval, the interval is added to the next fire time. Otherwise, the timer stops.

Note: in order for a timer to work consistently, you must keep a reference to it as long as it is in use. The easiest way is to make it a property of your script: this.timer = new Timer(this, this.doSomething, 5, 5). If this is not done, the JavaScript runtime may delete the timer at any time to free up memory.

Constructor

new Timer(this : Object, function : Function, delay : Number [, interval : Number]) : Timer

Creates a new timer which will call function after delay seconds, and optionally repeatedly every interval seconds. If delay is zero or more, the timer will be started automatically. If interval is 0 or less, the timer will not repeat. If interval is greater than 0 but less than 0.25, it will be rounded up to 0.25.

Properties

interval

interval : Number (read/write)

The rate at which the timer repeats. For a one-shot timer, this will be -1. If set to 0 or a negative number, it will be treated as -1. If set to a number between 0 and 0.25, it will be rounded up to 0.25.

isPersistent

isPersistent : Boolean (read/write)

A persistent timer will continue to run when the game resets (i.e., the player dies or loads a new game). A non-persistent timer will be stopped when the game is reset (but can be restarted using start()). Timers are not persistent by default.

isRunning

isRunning : Boolean (read-only)

true if the timer is running, false if it is stopped.

nextTime

nextTime : Number (read/write when stopped)

The next time the timer will fire, if it is running at that time. This can be modified if the timer is stopped. Note that this is an absolute time, not a delay. You can get the current absolute time using clock.absoluteSeconds.

Methods

start

start() : Boolean

Starts the timer, if it is not currently running. This will fail if it is a one-shot timer (i.e., its interval is -1) and its nextTime is in the past. Returns true if successful or the timer was already running, false on failure (i.e., the same as isRunning after start() is called).

stop

stop()

Stops the timer, if it is currently running.