LittleJS 1.31.0

Engine

Timer

Timer object tracks how long has passed since it was set

Constructor

new Timer(timeLeft, useRealTime)engineUtilities.js:28

Create a timer object set time passed in

timeLeft number optional

How much time left before the timer is elapsed in seconds (undefined = unset)

useRealTime boolean optional, default false

Should the timer keep running even when the game is paused? (useful for UI)

let a = new Timer;    // creates a timer that is not set
a.set(3);             // sets the timer to 3 seconds

let b = new Timer(1); // creates a timer with 1 second left
b.unset();            // unset the timer

Members

Methods

Members

setTime : number|undefinedengineUtilities.js:42
time : number|undefinedengineUtilities.js:40
useRealTime : booleanengineUtilities.js:37

Runs on real time, so it keeps going while the game is paused, see setUseRealTime

Methods

active() → booleanengineUtilities.js:72

Returns true if set and has not elapsed

elapsed() → booleanengineUtilities.js:77

Returns true if set and elapsed, on the frame it ends though the time it was set at and its seconds round apart, so a timer of a whole number of frames takes that many

get() → numberengineUtilities.js:82

Get how long since elapsed, returns 0 if not set (returns negative if currently active), 0 on the frame it ends, as elapsed() reads it

getGlobalTime() → numberengineUtilities.js:105

Get the current global time this timer is based on

getPercent() → numberengineUtilities.js:91

Get percentage elapsed based on time it was set to, returns 0 if not set. Zero-duration timers report 1 (already elapsed).

getSetTime() → numberengineUtilities.js:101

Get the time this timer was set to, returns 0 if not set

isSet() → booleanengineUtilities.js:68

Returns true if set

Set the timer with seconds passed in

timeLeft number optional, default 0

How much time left before the timer is elapsed in seconds

setUseRealTime(useRealTime)engineUtilities.js:57

Set if the timer should keep running even when the game is paused

useRealTime boolean optional, default true
toString() → stringengineUtilities.js:109

Returns this timer expressed as a string

Unset the timer

valueOf() → numberengineUtilities.js:113

Get how long since elapsed, returns 0 if not set (returns negative if currently active)