直接上代碼,
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; } } }