我有一个函数,它的数据位于 for 循环中,数据被传递到我的函数中,该函数将数据放入 URL 加载器,加载它,然后从加载的 URL 返回数据。这不起作用,就好像我尝试不停地这样做一样,URL 加载速度不够快,无法返回数据。我找不到停止的方法,所以它不会导致错误。我不能使用事件侦听器,因为它们不能返回值,而且我不能只创建一个函数来将数据分配给一个变量,因为在使用 URLloader 调用该函数的函数中需要它。我可以做一个图表来更好地解释。如果我需要以某种方式重组我的代码,我不太确定这是否可能。我试过这两种方法:这会导致堆栈溢出:
package app
{
import flash.xml.XMLDocument;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.MovieClip;
public class main extends MovieClip
{
var geoReq:URLRequest = new URLRequest;
var geoLoader:URLLoader = new URLLoader();
var GeocodeResponse:XML;
var geoLocList:XMLList = new XMLList();
var geoLoc:Array = new Array();
function main():void
{
var urlReq:URLRequest = new URLRequest("http://search.twitter.com/search.atom?q=flu&rpp=3&lang=en&geocode=55.378051,-3.435973,605mi");
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, getTweets);
loader.load(urlReq);
}
function getTweets( e:Event ):void
{
if ( e.target.data )
{
var tweets = new Array(); var times = new Array();
var twitterXML:XML = new XML(e.target.data);
var tweetList:XMLList = twitterXML.children();
var tweetItem:String;
var placeItem:String;
var tweet:Array = new Array();
for (var i:int = 0; i < tweetList.length(); i++)
{
tweetItem = tweetList[i].*::title;
placeItem = tweetList[i].*::location;
if( tweetItem && placeItem != "")
{
placeItem = loadGeo(placeItem);
tweet = [tweetItem, placeItem];
tweets.push(tweet);
trace(tweets[tweets.length - 1][0]);
trace(tweets[tweets.length - 1][1]);
}
}
trace(tweets.length);
}
}
function loadGeo(loc:String):Array
{
geoReq = new URLRequest("https://maps.googleapis.com/maps/api/geocode/xml?address="+loc+"&sensor=false");
geoLoader.load(geoReq);
GeocodeResponse = new XML(geoLoader.data);
geoLoc = returnLoc();
return geoLoc;
}
function returnLoc():Array
{
if(geoLoader.data == null)
{
returnLoc();
}
else
{
var returnD:Array = new Array(GeocodeResponse.result.geometry.location[0].lat, GeocodeResponse.result.geometry.location[0].lng);
}
return returnD;
}
}
}
这会导致错误“TypeError: Error #1010: A term is undefined and has no properties.”:
package app
{
import flash.xml.XMLDocument;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.MovieClip;
public class main extends MovieClip
{
var geoReq:URLRequest = new URLRequest;
var geoLoader:URLLoader = new URLLoader();
var GeocodeResponse:XML;
var geoLocList:XMLList = new XMLList();
var geoLoc:Array = new Array();
function main():void
{
var urlReq:URLRequest = new URLRequest("http://search.twitter.com/search.atom?q=flu&rpp=3&lang=en&geocode=55.378051,-3.435973,605mi");
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, getTweets);
loader.load(urlReq);
}
function getTweets( e:Event ):void
{
if ( e.target.data )
{
var tweets = new Array(); var times = new Array();
var twitterXML:XML = new XML(e.target.data);
var tweetList:XMLList = twitterXML.children();
var tweetItem:String;
var placeItem:String;
var tweet:Array = new Array();
for (var i:int = 0; i < tweetList.length(); i++)
{
tweetItem = tweetList[i].*::title;
placeItem = tweetList[i].*::location;
if( tweetItem && placeItem != "")
{
placeItem = loadGeo(placeItem);
tweet = [tweetItem, placeItem];
tweets.push(tweet);
trace(tweets[tweets.length - 1][0]);
trace(tweets[tweets.length - 1][1]);
}
}
trace(tweets.length);
}
}
function loadGeo(loc:String):Array
{
geoReq = new URLRequest("https://maps.googleapis.com/maps/api/geocode/xml?address="+loc+"&sensor=false");
geoLoader.load(geoReq);
GeocodeResponse = new XML(geoLoader.data);
geoLoc = [GeocodeResponse.result.geometry.location[0].lat, GeocodeResponse.result.geometry.location[0].lng];
return geoLoc;
}
}
}
请帮助,提前谢谢,凯尔。