Skip to main content

timer

May 5, 2020

timer

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

Examples:

JavaScript
// Add a timer
timer.add(1000, 'test');
// Or:
new CtTimer(1000, 'test');
JavaScript
// Create a new timer and remember it in a variable `timer`
// Log "Done!" when it gets to 2.5 seconds
var timer = 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();

timer methods

timer.add(timeMs, name) ⇒ void

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

ParamTypeDescription
timeMsNumberThe length of the timer, in milliseconds
[name]StringThe timer's name, which will be accessible from timer.name.

timer.addUi(timeMs, name) ⇒ void

Creates a new timer that runs in UI time scale.

ParamTypeDescription
timeMsNumberThe length of the timer, in milliseconds
[name]StringThe 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 u.timeUi for counting time. if false, it will use u.time 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.

ParamTypeDescription
onfulfilledAnyThe callback to execute when the Promise is resolved.
[onrejected]AnyThe callback to execute when the Promise is rejected.

CtTimer.catch ⇒ void

Mirrors CtTimer.promise.catch().

Attaches callbacks for the rejection of the internal Promise.