直接上代码,
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; } } }
Egret定时器实现倒计时
本文详细介绍了如何使用Egret框架中的定时器组件实现倒计时功能,包括三种不同的实现方法:通过监听计时事件更新显示、设置定时器执行次数达到指定倒计时效果以及使用setInterval函数配合条件判断实现精确计时。
588

被折叠的 条评论
为什么被折叠?



