直接上代码,
1private timeControl() { 2 let timer: egret.Timer = segret.Timer(1000); 3 timer.addEventListener(egret.TimerEvent.TIMER,(event:egret.TimerEvent) =>{ 4 this.countTotalTime--; 5 if(this.countTotalTime < 0){ 6 //this.countDownShow.text = "0"; 7 return; 8 } 9 this.countDownShow.text= this.countTotalTime.toString(); 10 }, this); 11 timer.start(); 12 13 } 14 15二、 16var count:number = 60; 17var timer:egret.Timer = new egret.Timer(1000,60);//1000代表1秒执行一次,60代表执行60次,这样实现的一分钟计时 18timer.addEventListener(egret.TimerEvent.TIMER,onTimer,this); 19timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE,onTimerComplete,this); 20timer.start(); 21function onTimer(evt:egret.TimerEvent):void { 22 count--; 23 console.log("倒计时:"+count); 24} 25function onTimerComplete(evt:egret.TimerEvent):void { 26 console.log("结束"); 27} 28三、 29public countDownShow: eui.Label; 30private timer; 31private timeControl(second) { 32 if (second > 0) { 33 this.countDownShow.visible = true; 34 this.timer = egret.setInterval(function () { 35 if (second > 1) { 36 second--; 37 this.countDownShow.text = second.toString(); 38 } 39 }, this, 1000); 40 if (second <= 1) { 41 console.log("停止计时"); 42 clearInterval(this.timer); 43 this.countDownShow.visible = false; 44 } 45 } 46 47 }