加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex之无延时定时器

发布时间:2020-12-15 01:09:06 所属栏目:百科 来源:网络整理
导读:在写定时器的时候,我们希望第一次开启定时器不用延时,直接加载。 而不用再等待该定时器设定的时间就直接运行。也是就说无延时定时器 下面我举个简单的例子加以说明。 MyTimer.as: package com.utils.allas{import flash.utils.Timer;import flash.events.T

在写定时器的时候,我们希望第一次开启定时器不用延时,直接加载。

而不用再等待该定时器设定的时间就直接运行。也是就说无延时定时器

下面我举个简单的例子加以说明。

MyTimer.as:

package com.utils.allas
{
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	public class MyTimer extends Timer {
		private var startDelay:Boolean;
		public function MyTimer(delay:Number,repeatCount:int = 0,startDelay:Boolean = true) {
			this.startDelay = startDelay;
			super(delay,repeatCount);
		}
		public override function start():void {
			if (!startDelay) dispatchEvent(new TimerEvent(TimerEvent.TIMER));
			super.start();
		}
	}
}

TestTimer.as:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
			   initialize="application1_initializeHandler(event)"
			   
			   >
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	<fx:Script>
		<![CDATA[
			import com.utils.allas.MyTimer;
			
			import mx.events.FlexEvent;
			
			import  flash.events.TimerEvent;
			import mx.controls.Alert;
			
			protected function application1_initializeHandler(event:FlexEvent):void
			{
				// TODO Auto-generated method stub
				var timer:MyTimer = new MyTimer(5000,5,false);
				timer.addEventListener(TimerEvent.TIMER,timerHandler);
				timer.start();
			}
			
			public function timerHandler(event:TimerEvent):void{
				Alert.show("你好");
			}
			
		]]>
	</fx:Script>
</s:Application>

注意:

??不延迟第一次调度timer事件不会计入到计时器的总运行次数repeatCount的。即调度完第一次timer事件后,currentCount仍为0。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读