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();
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 . |
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 . |
The amount of time the timer has been active, in milliseconds.
The amount of time left until it gets to timeMs
. Defaults to 0
.
The given name of a timer, or false
if no name was given.
If true
, it will use ct.deltaUi
for counting time. if false
, it will use ct.delta
for counting time.
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.
Instantly triggers the promise, calling its callback.
Stops the timer by rejecting the internal promise.
If true, the timer was rejected.
If true, the timer was resolved.
If true, the timer was either rejected or resolved.
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. |
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. |