package cagWeb.service.properties.util { import flash.events.Event; import flash.events.IOErrorEvent; import flash.events.ProgressEvent; import flash.events.SecurityErrorEvent; import flash.events.TimerEvent; import flash.net.URLLoader; import flash.net.URLRequest; import flash.utils.Timer; /** * 带超时处理的urlLoader * @author */ public class URLLoader extends flash.net.URLLoader { private var _timeoutTimer:Timer; public var timeout:Number; // default timeout value public static const TIMEOUT:String = 'loaderTimeout'; [Event(name="loaderTimeout",type="flash.events.Event")] public function URLLoader(timeout:Number = 1000,request:URLRequest=null):void { super(request); this.timeout = timeout; _timeoutTimer = new Timer(timeout); } override public function load(request:URLRequest):void { _timeoutTimer.addEventListener(TimerEvent.TIMER,handleTimeout); _timeoutTimer.delay = timeout; addEventListener(IOErrorEvent.IO_ERROR,handleLoadActivity); addEventListener(SecurityErrorEvent.Security_ERROR,handleLoadActivity); addEventListener(Event.COMPLETE,handleLoadActivity); addEventListener(ProgressEvent.PROGRESS,handleLoadActivity); addEventListener(Event.OPEN,handleLoadActivity); super.load(request); _timeoutTimer.start(); } override public function close():void { killTimer(); super.close(); } private function handleLoadActivity(event:Event):void { killTimer(); } private function killTimer(event:Event = null):void { removeEventListener(IOErrorEvent.IO_ERROR,handleLoadActivity); removeEventListener(SecurityErrorEvent.Security_ERROR,handleLoadActivity); removeEventListener(Event.COMPLETE,handleLoadActivity); removeEventListener(ProgressEvent.PROGRESS,handleLoadActivity); removeEventListener(Event.OPEN,handleLoadActivity); _timeoutTimer.reset(); _timeoutTimer.removeEventListener(TimerEvent.TIMER,handleTimeout); if(event) super.dispatchEvent(event.clone()); } private function handleTimeout(event:TimerEvent):void { killTimer(); super.dispatchEvent(new Event(TIMEOUT,true)); // this.dispatchEvent(new Event(TIMEOUT,true)); // var dis:Eventdispatcher = new Eventdispatcher(this); // dis.dispatchEvent(new Event(TIMEOUT,true)); } } }
具备请求url的超时功能。
var urlload:UrlLoader = new UrlLoader(); urlLoad.timerout=2000; urlLoad.addEventListener(URLLoader.TIMEOUT,timerOutHandler);