# ct.timer

ct.timer allows for making timers, optionally running a function when the timer reaches a certain amount of time.

Examples:

// Add a timer
ct.timer.add(1000, 'test');
// Or:
new CtTimer(1000, 'test');
// Create a new timer and remember it in a variable `timer`
// Log "Done!" when it gets to 2.5 seconds
var timer = ct.timer.add(2500, 'test');
timer.then(() => {
    // Do something useful
    hero.invincible = false;
    console.log('Done!');
})
// The `catch` part is not necessary. Without it, though, you will
// see errors in the console when timers got interrupted,
// either manually or when you switch rooms
.catch(e => {
    console.log('Timer removed', e);
    // You can add code here so that important stuff still
    // gets executed on room switch:
    hero.invincible = false;
});

// Log how much time left
console.log(timer.time);

// Stop the timer. It won't call the code inside `then(() => {})` clause
timer.reject();

// Trigger the timer manually
timer.resolve();

# ct.timer methods

# ct.timer.add(timeMs, name) ⇒ void

Creates a new timer that runs in gameplay time scale and is affected by time acceleration/deceleration.

Param Type Description
timeMs Number The length of the timer, in milliseconds
[name] String The timer's name, which will be accessible from timer.name.

# ct.timer.addUi(timeMs, name) ⇒ void

Creates a new timer that runs in UI time scale.

Param Type Description
timeMs Number The length of the timer, in milliseconds
[name] String The timer's name, which will be accessible from timer.name.

# Timer properties

# CtTimer.time ⇒ Number

The amount of time the timer has been active, in milliseconds.

# CtTimer.timeLeft ⇒ Number

The amount of time left until it gets to timeMs. Defaults to 0.

# CtTimer.name ⇒ String|false

The given name of a timer, or false if no name was given.

# CtTimer.uiDelta ⇒ Boolean

If true, it will use ct.deltaUi for counting time. if false, it will use ct.delta for counting time.

# CtTimer.promise ⇒ Promise

The promise used to execute callbacks when the timer has finished. You can use it with other promises and Promise methods to create complex asynchronous chains.

# CtTimer.resolve ⇒ Function

Instantly triggers the promise, calling its callback.

# CtTimer.reject ⇒ Function

Stops the timer by rejecting the internal promise.

# CtTimer.rejected ⇒ Boolean

If true, the timer was rejected.

# CtTimer.done ⇒ Boolean

If true, the timer was resolved.

# CtTimer.settled ⇒ Boolean

If true, the timer was either rejected or resolved.

# Timer methods

# CtTimer.then ⇒ void

Mirrors CtTimer.promise.then().

Attaches callbacks for the resolution and/or rejection of the internal Promise.

Param Type Description
onfulfilled Any The callback to execute when the Promise is resolved.
[onrejected] Any The callback to execute when the Promise is rejected.

# CtTimer.catch ⇒ void

Mirrors CtTimer.promise.catch().

Attaches callbacks for the rejection of the internal Promise.

Param Type Description
onfulfilled Any The callback to execute when the Promise is resolved.
[onrejected] Any The callback to execute when the Promise is rejected.