Permalink
Browse files

added timeout functionality to asyncRequest

  • Loading branch information...
1 parent 4fc1d00 commit 2e12ae0d3cfd01cd950b63db8c601495bdaf318d @zagraves zagraves committed Jul 23, 2009
Showing with 31 additions and 1 deletion.
  1. +31 −1 Source/com/yahoo/net/Connection.as
@@ -84,8 +84,10 @@ package com.yahoo.net
*
* @return The URLRequest object generated.
*/
- public static function asyncRequest(httpMethod:String, url:String, callback:Object, args:Object=null, headers:Array=null):URLRequest
+ public static function asyncRequest(httpMethod:String, url:String, callback:Object, args:Object=null, headers:Array=null, timeout:int=-1):URLRequest
{
+ var hasResponse:Boolean;
+ var timedOut:Boolean;
var status:int;
var responseHeaders:Array;
var urlLoader:URLLoader;
@@ -142,6 +144,7 @@ package com.yahoo.net
urlLoader.addEventListener(Event.COMPLETE, function(event:Event):void
{
+ hasResponse = true;
var responseText:String = urlLoader.data;
var responseXML:XML;
var parseSuccess:Boolean=true;
@@ -204,6 +207,33 @@ package com.yahoo.net
}
});
+ if(timeout > 0) {
+ flash.utils.setTimeout(function ():void
+ {
+ if(!hasResponse) {
+ urlLoader.close();
+ timedOut = true;
+ var data:Object = {
+ status:status,
+ responseHeaders:responseHeaders,
+ args:args,
+ url:url,
+ headers:headers,
+ method:httpMethod,
+ bytesLoaded: urlLoader.bytesLoaded,
+ bytesTotal: urlLoader.bytesTotal,
+ timedOut:timedOut
+ };
+ if(callback.timeout && typeof callback.timeout == FUNCTION) {
+ callback.timeout(data);
+ }
+ // if(callback.failure && typeof callback.failure == FUNCTION) {
+ // callback.failure(data);
+ // }
+ }
+ }, timeout);
+ }
+
var request:URLRequest = new URLRequest(url);
var n:String = null;

0 comments on commit 2e12ae0

Please sign in to comment.