ct.timer
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');
# 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();
# Create a new timer and remember it in a variable `timer`
# Log "Done!" when it gets to 2.5 seconds
timer = ct.timer.add 2500, 'test'
timer.then =>
# Do something useful
hero.invincible = false
console.log 'Done!'
.catch (e) =>
# You can add code here so that important stuff still
# gets executed on room switch:
console.log 'Timer removed', e
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
void
ct.timer.add(timeMs, name) ⇒ 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 . |
void
ct.timer.addUi(timeMs, name) ⇒ 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
Number
CtTimer.time ⇒ The amount of time the timer has been active, in milliseconds.
Number
CtTimer.timeLeft ⇒ 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.
Boolean
CtTimer.uiDelta ⇒ If true
, it will use ct.deltaUi
for counting time. if false
, it will use ct.delta
for counting time.
Promise
CtTimer.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.
Function
CtTimer.resolve ⇒ Instantly triggers the promise, calling its callback.
Function
CtTimer.reject ⇒ Stops the timer by rejecting the internal promise.
Boolean
CtTimer.rejected ⇒ If true, the timer was rejected.
Boolean
CtTimer.done ⇒ If true, the timer was resolved.
Boolean
CtTimer.settled ⇒ If true, the timer was either rejected or resolved.
Timer methods
void
CtTimer.then ⇒ 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. |
void
CtTimer.catch ⇒ 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. |