Skip to content
TimeUtil

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

ts
@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 numberdelayExecute 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

ts
@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 numbersetInterval 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

ts
@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 () => voidExecution method
frameNum? numberThe 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

numberID 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

ts
@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 numberTime (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

ts
@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

numberNumber (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

numberThe 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

ts
@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 DateUsage: Standard time, timestamp, etc
format? stringUsage: date string default: outer range: format of incoming time, such as: January 20, 1996

Returns

stringTime 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

ts
@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 () => voidMethod to be executed
timeout numberInterval time (minimum time is the difference between two frames in seconds)
range: [0,+∞] type: floating-point number
exitJudge? () => booleanThe exit judgment method returns true and stops default: null

Returns

numberID 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

ts
@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

ts
@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}`);
    }

}