我有一个用 AS3 编写的简单倒计时计时器,它随着时间的推移逐渐增加 CPU 使用和帧渲染时间,直到帧速率在 10 分钟内下降到每秒 2-3 帧。时间线有 1 帧,舞台有 4 个 tlf 文本区域,实例名称为 HH MM SS 和 FF。代码根据与当前时间和“事件时间”的比较,在每一帧更新这些文本区域。
我查看了侦察,违规活动是“处理事件'渲染'”,它占用了 95% 的活动时间
代码如下所示
import flash.events.Event;
stop();
// ***Customization Point*** Enter the event date as (Year, Month - 1, day, 24Hr Hours, Minutes -1, Seconds - 1)
var End:Date = new Date(2013, 9, 15, 12, 0, 0);
var Now:Date;
var HH:Number;
var MM:Number;
var SS:Number;
var FF:Number;
stage.addEventListener(Event.ENTER_FRAME, tick);
function tick(e:Event = null):void
{
Now = new Date();
HH = (End.day > Now.day) ? (End.hours - Now.hours + (24 * (End.day - Now.day))) : (End.hours - Now.hours);
MM = (End.minutes == 0)? 60 - Now.minutes : End.minutes - Now.minutes;
MM = (MM == 60)? 0 : MM;
HH = (MM < 0)? HH - 1 : HH - 1;
MM = (MM < 0)? -1 * MM : MM;
SS = (End.seconds == 0)? 60 - Now.seconds : End.seconds - Now.seconds;
SS = (SS == 60)? 0 : SS;
MM = (SS < 0)? MM - 1 : MM;
SS = (SS < 0)? -1 * SS : SS;
FF = 30 - Math.round(Now.milliseconds / 33.33);
F.text = (FF < 10) ? "0" + FF.toString() : FF.toString();
S.text = (SS < 10) ? "0" + SS.toString() : SS.toString();
M.text = (MM < 10) ? "0" + MM.toString() : MM.toString();
H.text = (HH < 10) ? "0" + HH.toString() : HH.toString();
}