带超时的urlLoader

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);

相关文章

  译序:JWMediaPlayer是开源的网页使用的Flash播放器。本...
    Flash编程原理都是只能将1写为0,而不能将0写成1.所...
 上传setenvgatewayip192.168.1.1;setenvserverip192.168.1...
Error:FlashDownloadFailed-"Cortex-M3"出现一般有...
jPlayer是一个用于控制和播放mp3文件的jQuery插件。它在后台...
#ifndef__FONTUPD_H__#define__FONTUPD_H__#include"sy...