Utils / TimeUtil
TimeUtil Class
Time tool
Table of contents
Properties
onEnterFrame: Action1 <number > |
---|
Frame refresh event (parameter deltaTime) |
traceFrameTime: boolean |
Do you output the execution time for each frame |
Methods
clearDelayExecute(id : number ): void other |
---|
Clear delayExecute |
clearInterval(id : number ): void other |
Clear setInterval |
delayExecute(handler : () => void , frameNum? : number ): number other |
Delay execution method by a certain number of frames |
delaySecond(second : number ): Promise <void > other |
Delay for a certain number of seconds, used for waiting in the middle of asynchronous methods |
deltatime(): number other |
Time elapsed per frame (unit: seconds) |
elapsedTime(): number other |
Returns the total time elapsed since the game was run, measured in seconds and accurate to milliseconds. |
parseTime(timeData : Date , format? : string ): string other |
Format timestamp |
setInterval(handler : () => void , timeout : number , exitJudge? : () => boolean ): number other |
Execute the method at certain time intervals |
time(): number other |
Returns the time (in seconds) that has passed since the UNIX Epoch on the computer where the current local session is located. |
Properties
onEnterFrame
▪ Static
onEnterFrame: Action1
<number
>
Frame refresh event (parameter deltaTime)
Precautions
Automatically execute the bound method every update
Example usage: Create a script named TimeExample, place it in the object bar, open the script, modify the original content to the following, save and run the game, and output dt per frame
@Component
export default class TimeExample extends Script {
protected onStart(): void {
this.test();
}
private async test(): Promise<void> {
if (!SystemUtil.isClient()) return;
TimeUtil.onEnterFrame.add(this.onEnterFrame, this);
}
private onEnterFrame(dt: number): void {
console.log("dt", dt);
}
}
@Component
export default class TimeExample extends Script {
protected onStart(): void {
this.test();
}
private async test(): Promise<void> {
if (!SystemUtil.isClient()) return;
TimeUtil.onEnterFrame.add(this.onEnterFrame, this);
}
private onEnterFrame(dt: number): void {
console.log("dt", dt);
}
}
traceFrameTime
▪ Static
traceFrameTime: boolean
Do you output the execution time for each frame
Methods
clearDelayExecute
• Static
clearDelayExecute(id
): void
other
Clear delayExecute
Parameters
id number | delayExecute The ID returned by the method range: depends on the length of the ID. Type: integer |
---|
Usage example: create a script named TimeExample, place it in the object bar, open the script, modify the original content to the following, save and run the game, which would have delayed execution by 600 frames, but will not be executed now
@Component
export default class TimeExample extends Script {
protected onStart(): void {
this.test();
}
private async test(): Promise<void> {
if (!SystemUtil.isClient()) return;
let id = TimeUtil.delayExecute(() => {
//Delay execution of pie environment by 600 frames for approximately 10 seconds
console.log("delay 600execute");
}, 600)
TimeUtil.clearDelayExecute(id);
//Clear delayed execution
}
}
@Component
export default class TimeExample extends Script {
protected onStart(): void {
this.test();
}
private async test(): Promise<void> {
if (!SystemUtil.isClient()) return;
let id = TimeUtil.delayExecute(() => {
//Delay execution of pie environment by 600 frames for approximately 10 seconds
console.log("delay 600execute");
}, 600)
TimeUtil.clearDelayExecute(id);
//Clear delayed execution
}
}
clearInterval
• Static
clearInterval(id
): void
other
Clear setInterval
Parameters
id number | setInterval The ID returned by the method range: depends on the length of the ID. Type: Floating point number |
---|
Usage example: create a script named TimeExample, place it in the object bar, open the script, modify the original content to the following, save and run the game, which originally would output the id every 2 seconds until the id>5, and stop in advance after press the F key
@Component
export default class TimeExample extends Script {
protected onStart(): void {
this.test();
}
private async test(): Promise<void> {
if (!SystemUtil.isClient()) return;
let isInterval = false;
let id = 0;
let ineterval = TimeUtil.setInterval(() => {
console.log(id);
id++;
if (id > 5) {
isInterval = true;
}
}, 2, () => {
return isInterval;
})
InputUtil.onKeyDown(Keys.F, () => {
TimeUtil.clearInterval(ineterval);
})
}
}
@Component
export default class TimeExample extends Script {
protected onStart(): void {
this.test();
}
private async test(): Promise<void> {
if (!SystemUtil.isClient()) return;
let isInterval = false;
let id = 0;
let ineterval = TimeUtil.setInterval(() => {
console.log(id);
id++;
if (id > 5) {
isInterval = true;
}
}, 2, () => {
return isInterval;
})
InputUtil.onKeyDown(Keys.F, () => {
TimeUtil.clearInterval(ineterval);
})
}
}
delayExecute
• Static
delayExecute(handler
, frameNum?
): number
other
Delay execution method by a certain number of frames
Parameters
handler () => void | Execution method |
---|---|
frameNum? number | The number of frames to be delayed default: 1 range: It depends on the number of frames you want to delay, without any restrictions. Type: integer |
Returns
number | ID used for stopping |
---|
Usage example: create a script named TimeExample, place it in the object bar, open the script, modify the original content to the following, save and run the game, and the execution will be delayed by 600 frames
@Component
export default class TimeExample extends Script {
protected onStart(): void {
this.test();
}
private async test(): Promise<void> {
if (!SystemUtil.isClient()) return;
TimeUtil.delayExecute(() => {
//Delay execution of pie environment by 600 frames for approximately 10 seconds
console.log("delay 600execute");
}, 600)
}
}
@Component
export default class TimeExample extends Script {
protected onStart(): void {
this.test();
}
private async test(): Promise<void> {
if (!SystemUtil.isClient()) return;
TimeUtil.delayExecute(() => {
//Delay execution of pie environment by 600 frames for approximately 10 seconds
console.log("delay 600execute");
}, 600)
}
}
delaySecond
• Static
delaySecond(second
): Promise
<void
> other
Delay for a certain number of seconds, used for waiting in the middle of asynchronous methods
Parameters
second number | Time (unit: seconds) range: unlimited type: floating-point number |
---|
Returns
Promise <void > | Promise |
---|
Usage example: create a script named TimeExample, place it in the object bar, open the script, modify the original content to the following, save and run the game, and output 5 seconds later in 5 seconds
@Component
export default class TimeExample extends Script {
protected onStart(): void {
this.test();
}
private async test(): Promise<void> {
if (!SystemUtil.isClient()) return;
TimeUtil.delaySecond(5).then(() => {
console.log("5 seconds later");
})
}
}
@Component
export default class TimeExample extends Script {
protected onStart(): void {
this.test();
}
private async test(): Promise<void> {
if (!SystemUtil.isClient()) return;
TimeUtil.delaySecond(5).then(() => {
console.log("5 seconds later");
})
}
}
deltatime
• Static
deltatime(): number
other
Time elapsed per frame (unit: seconds)
Returns
number | Number (in seconds) |
---|
Precautions
The interval between two call to the Update function before call this function
elapsedTime
• Static
elapsedTime(): number
other
Returns the total time elapsed since the game was run, measured in seconds and accurate to milliseconds.
Returns
number | The total time elapsed since the game was run. |
---|
Precautions
In Editor, this value is calculated from each start of running, rather than from opening the Editor scene.
Example usage: Create a script called TimeExample, place it in the object bar, open the script, modify the original content to the following, save and run the game, press F to output the total time the game runs
@Component
export default class TimeExample extends Script {
protected onStart(): void {
this.test();
}
private async test(): Promise<void> {
if (!SystemUtil.isClient()) return;
InputUtil.onKeyDown(Keys.F, () => {
const elapsedTime = TimeUtil.elapsedTime();
console.log(`The game ran for ${elapsedTime} seconds`);
});
}
}
@Component
export default class TimeExample extends Script {
protected onStart(): void {
this.test();
}
private async test(): Promise<void> {
if (!SystemUtil.isClient()) return;
InputUtil.onKeyDown(Keys.F, () => {
const elapsedTime = TimeUtil.elapsedTime();
console.log(`The game ran for ${elapsedTime} seconds`);
});
}
}
parseTime
• Static
parseTime(timeData
, format?
): string
other
Format timestamp
Parameters
timeData Date | Usage: Standard time, timestamp, etc |
---|---|
format? string | Usage: date string default: outer range: format of incoming time, such as: January 20, 1996 |
Returns
string | Time string after formatting |
---|
Example usage: Create a script named TimeExample, place it in the object bar, open the script, modify the original content to the following, save and run the game, and the current time will be output
@Component
export default class TimeExample extends Script {
protected onStart(): void {
this.test();
}
private async test(): Promise<void> {
if (!SystemUtil.isClient()) return;
const time = TimeUtil.parseTime(new Date());
console.log(`time:${time}`);
}
}
@Component
export default class TimeExample extends Script {
protected onStart(): void {
this.test();
}
private async test(): Promise<void> {
if (!SystemUtil.isClient()) return;
const time = TimeUtil.parseTime(new Date());
console.log(`time:${time}`);
}
}
setInterval
• Static
setInterval(handler
, timeout
, exitJudge?
): number
other
Execute the method at certain time intervals
Parameters
handler () => void | Method to be executed |
---|---|
timeout number | Interval time (minimum time is the difference between two frames in seconds) range: [0,+∞] type: floating-point number |
exitJudge? () => boolean | The exit judgment method returns true and stops default: null |
Returns
number | ID used for stopping |
---|
Usage example: create a script named TimeExample, place it in the object bar, open the script, modify the original content to the following, save and run the game, and output the id every 2 seconds until the id>5
@Component
export default class TimeExample extends Script {
protected onStart(): void {
this.test();
}
private async test(): Promise<void> {
if (!SystemUtil.isClient()) return;
let isInterval = false;
let id = 0;
TimeUtil.setInterval(() => {
console.log(id);
id++;
if (id > 5) {
isInterval = true;
}
}, 2, () => {
return isInterval;
})
}
}
@Component
export default class TimeExample extends Script {
protected onStart(): void {
this.test();
}
private async test(): Promise<void> {
if (!SystemUtil.isClient()) return;
let isInterval = false;
let id = 0;
TimeUtil.setInterval(() => {
console.log(id);
id++;
if (id > 5) {
isInterval = true;
}
}, 2, () => {
return isInterval;
})
}
}
time
• Static
time(): number
other
Returns the time (in seconds) that has passed since the UNIX Epoch on the computer where the current local session is located.
Returns
number | (UNIX The number of seconds that have passed since the beginning of the era. |
---|
Precautions
UNIX The starting date of the era is January 1, 1970.
Example usage: Create a script named TimeExample, place it in the object bar, open the script, modify the original content to the following, save and run the game, and the current timestamp will be output
@Component
export default class TimeExample extends Script {
protected onStart(): void {
this.test();
}
private async test(): Promise<void> {
if (!SystemUtil.isClient()) return;
const time = TimeUtil.time();
console.log(`time stamp:${time}`);
}
}
@Component
export default class TimeExample extends Script {
protected onStart(): void {
this.test();
}
private async test(): Promise<void> {
if (!SystemUtil.isClient()) return;
const time = TimeUtil.time();
console.log(`time stamp:${time}`);
}
}