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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。