Egret引擎的常用倒计时

直接上代码,

private timeControl() { 
        let timer: egret.Timer = segret.Timer(1000);
        timer.addEventListener(egret.TimerEvent.TIMER,(event:egret.TimerEvent) =>{
            this.countTotalTime--;
            if(this.countTotalTime < 0){
                //this.countDownShow.text = "0";
                return;
            }
            this.countDownShow.text= this.countTotalTime.toString();
        }, this);
        timer.start();
         
    }

二、
var count:number = 60;
var timer:egret.Timer = new egret.Timer(1000,60);//1000代表1秒执行一次,60代表执行60次,这样实现的一分钟计时
timer.addEventListener(egret.TimerEvent.TIMER,onTimer,this);
timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE,onTimerComplete,this);
timer.start();
function onTimer(evt:egret.TimerEvent):void {
        count--;
        console.log("倒计时:"+count);
}
function onTimerComplete(evt:egret.TimerEvent):void {
        console.log("结束");
}
三、
public countDownShow: eui.Label;
private timer;
private timeControl(second) {
    if (second > 0) {
        this.countDownShow.visible = true;
        this.timer = egret.setInterval(function () {
            if (second > 1) {
                second--;
                this.countDownShow.text = second.toString();
                }
            }, this, 1000);
            if (second <= 1) {
                console.log("停止计时");
                clearInterval(this.timer);
                this.countDownShow.visible = false;
            }
        }

    }
原文地址:https://www.cnblogs.com/allyh/p/10434251.html