Detailed explanation of how to use several timers in CocosCreator

Detailed explanation of how to use several timers in CocosCreator

1. setTimeOut

Print abc after 3 seconds. Execute only once.

setTimeout(()=>{console.log("abc"); }, 3000);

Delete the timer and abc will not be output after 3 seconds.

let timeIndex;
timeIndex = setTimeout(()=>{console.log("abc"); }, 3000);
clearTimeout(timeIndex);

setTimeout is written like this, the this output in the test function is the Window object

@ccclass
export default class Helloworld extends cc.Component {
 
    private a = 1;
 
    start() {
        setTimeout(this.test, 3000);
    }
 
    private test(){
        console.log(this.a); //output undefined
        console.log(this); //Window
    }
}

Using Arrow Functions

@ccclass
export default class Helloworld extends cc.Component {
 
    private a = 1;
 
    start() {
        setTimeout(()=>{this.test()}, 3000);
    }
 
    private test(){
        console.log(this.a); //output 1
        console.log(this); //Helloworld
    }
}

2. setInterval

Output abc after 1 second. Repeat the process and output abc every second.

setInterval(()=>{console.log("abc"); }, 1000);

Delete the timer and abc will no longer be output.

let timeIndex;
timeIndex = setInterval(()=>{console.log("abc"); }, 1000);
clearInterval(timeIndex);

Schedule

Each inherited cc.Component has this timer

schedule(callback: Function, interval?: number, repeat?: number, delay?: number): void;

After a delay of 3 seconds, abc is output, and then abc is output every 1 second, and repeated 5 times. So the final output will be 5+1 times abc.

this.schedule(()=>{console.log("abc")},1,5,3);

Delete schedule (if you want to delete it, you can no longer use anonymous functions, you must be able to access the function to be deleted)

private count = 1;
 
start() {
     
    this.schedule(this.test,1,5,3);
 
    this.unschedule(this.test);
}
 
private test(){
    console.log(this.count);
}

Global schedule

It is equivalent to a global timer on cc.director. Note that enableForTarget() must be called to register the id, otherwise an error will be reported.

start() {
    let scheduler:cc.Scheduler = cc.director.getScheduler();
    scheduler.enableForTarget(this);
    //After a delay of 3 seconds, output 1, and then output 1 every 1 second, repeat 3 times. A total of 1+3 outputs are output scheduler.schedule(this.test1, this, 1, 3,3, false);
    //After a delay of 3 seconds, output 1, and then output 1 every 1 second, repeating infinitely scheduler.schedule(this.test2, this, 1, cc.macro.REPEAT_FOREVER,3, false);
}
 
private test1(){
    console.log("test1");
}
 
private test2(){
    console.log("test2");
}
//Delete the timer scheduler.unschedule(this.test1, this);

The above is a detailed explanation of how to use several timers in CocosCreator. For more information about CocosCreator timers, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Unity3D realizes camera lens movement and limits the angle
  • CocosCreator learning modular script
  • How to use physics engine joints in CocosCreator
  • How to use JSZip compression in CocosCreator
  • CocosCreator Getting Started Tutorial: Making Your First Game with TS
  • Interpretation of CocosCreator source code: engine startup and main loop
  • CocosCreator general framework design resource management
  • How to make a List in CocosCreator
  • How to use http and WebSocket in CocosCreator
  • Analysis of CocosCreator's new resource management system
  • How to use cc.follow for camera tracking in CocosCreator

<<:  Detailed analysis of SQL execution steps

>>:  Nginx access control and parameter tuning methods

Recommend

Detailed explanation of how MySQL solves phantom reads

1. What is phantom reading? In a transaction, aft...

Summary of changes in the use of axios in vue3 study notes

Table of contents 1. Basic use of axio 2. How to ...

Linux CentOS6.5 yum install mysql5.6

This article shares the simple process of install...

Summary of MySQL database like statement wildcard fuzzy query

MySQL error: Parameter index out of range (1 >...

Docker View the Mount Directory Operation of the Container

Only display Docker container mount directory inf...

Thoroughly understand JavaScript prototype and prototype chain

Table of contents Preface Laying the foundation p...

MySQL trigger usage scenarios and method examples

trigger: Trigger usage scenarios and correspondin...

How to build YUM in Centos7 environment

1. Enter the configuration file of the yum source...

NodeJs high memory usage troubleshooting actual combat record

Preface This is an investigation caused by the ex...

Implementation of react loop data (list)

First, let's simulate the data coming from th...

W3C Tutorial (15): W3C SMIL Activities

SMIL adds support for timing and media synchroniz...

How to wrap HTML title attribute

When I was writing a program a few days ago, I wan...

Create a code example of zabbix monitoring system based on Dockerfile

Use the for loop to import the zabbix image into ...