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

HTML+CSS to achieve charging water drop fusion special effects code

Table of contents Preface: accomplish: Summarize:...

Detailed explanation of how to use Vue self-nested tree components

This article shares with you how to use the Vue s...

How to modify the initial password of a user in mysql5.7

When users install MySQL database for the first t...

Comparison between Redis and Memcache and how to choose

I've been using redis recently and I find it ...

How to solve mysql error 10061

This article shares with you the solution to the ...

Writing a web calculator using javascript

This article mainly records the effect of using j...

Sharing the structure and expression principles of simple web page layout

Introduction to structure and performance HTML st...

js to achieve star flash effects

This article example shares the specific code of ...

How to configure mysql on ubuntu server and implement remote connection

Server: Ubuntu Server 16.04 LSS Client: Ubuntu 16...

Element-ui's built-in two remote search (fuzzy query) usage explanation

Problem Description There is a type of query call...

Do you know why vue data is a function?

Official website explanation: When a component is...

A brief introduction to VUE uni-app basic components

1. scroll-view When using vertical scrolling, you...