Scheduler
Cocos Creator 3D provides a powerful, flexible and convenient timer component.
Using the Scheduler
Start a timer
this.schedule(function() { // Here this refers to component this.doSomething(); }, 5);
The above timer will be executed every 5s.
A more flexible timer
// time interval in seconds let interval = 5; // number of times to repeat let repeat = 3; // delay starts let delay = 10; this.schedule(function() { // Here this refers to component this.doSomething(); }, interval, repeat, delay);
The above timer will start timing after 10 seconds, and execute a callback every 5 seconds, repeating 3 times.
Timer that executes only once (shortcut)
this.scheduleOnce(function() { // Here this refers to component this.doSomething(); }, 2);
The timer above will execute the callback function once after two seconds, and then stop timing.
Cancel a timer
Developers can use the callback function itself to cancel the timer:
this.count = 0; this.callback = function () { if (this.count == 5) { // Cancel this timer when the callback is executed // for the sixth time this.unschedule(this.callback); } this.doSomething(); this.count++; } this.schedule(this.callback, 1);
Note: When the component's timer calls the callback, the
this
of the callback is specified as the component itself, sothis
can be used directly in the callback.
Here is a list of all of the timer functions in Component:
- schedule: start a timer
- scheduleOnce: start a timer that is executed only once
- unschedule: cancel a timer
- unscheduleAllCallbacks: cancel all timers of this component
The detailed description of these APIs can be found in the API documentation.
In addition, if developers need to execute a function every frame, please add the update
function directly to the Component. This function will be called every frame by default. This is described in Lifecycle Document.
Note:
Node
does not include timer related APIs