Flash AS3 locking up on per second XML call
I am writing a app that calls a price feed of 16 markets and displays them in the app.
In as1 there is no issue, it never locks up... but in as3 it makes maybe 20 calls (20 seconds of data) and then locks up.
I was thinking is there a buffering system or something like that I dont know about in as3
or any other ideas.
Here is the code
//===================================
// Package
//===================================
var priceFeedURL = "http://www.blabla.com/prices.xml";
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, showXML);
var myObj:Object = new Object();
//===================================
// Call XML
//===================================
function Init():void {
myTimer.start();
}
function callPriceFeed():void {
xmlLoader.load(new URLRequest(priceFeedURL));
}
function showXML(e:Event):void {
XML.ignoreWhitespace = true;
var pricesXML:XML = new XML(e.target.data);
myObj.currentPrice = pricesXML.IT[3].@BP;
PerSecondFunctions();
}
//===================================
// Timer
//===================================
var myTimer:Timer = new Timer(1000);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
function timerListener(e:TimerEvent):void {
callPriceFeed();
}
//===================================
// Per Second functions
//===================================
function PerSecondFunctions():void {
ShowPrice();
}
function ShowPrice():void {
开发者_如何学C currentPriceTXT.text = "PRICE : "+myObj.currentPrice;
trace(gnutradeObj.currentPrice+" "+Math.random());
priceGlowMC.gotoAndPlay(2);
}
Init();
Thanks
There are already some nice solutions (frameworks) for loading multiple files asynchronous:
http://code.google.com/p/bulk-loader/
http://www.greensock.com/loadermax/
I'd refer to The_asMan's comment about verifying that you really intend for HTTP requests to be sent once a second.
What I would do is try for an approach that serially requests data, mainly because you are using global vars to store/present data even though you also allow for multiple HTTP request/responses to be handled simultaneously. In this way, clients with different bandwidth capability could request data at most once per second and clients that lower bandwidth will not bottleneck their experience by re-requesting data before the UI has had a chance to render the latest response.
Basically, I stop the timer at one second, then on error or after response has been fully/mostly handled, start up the timer again.
My changes are annotated with // ADDED
xmlLoader.addEventListener(Event.COMPLETE, showXML);
xmlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
// ADDED - restart (or maybe don't restart?) on error
function errorHandler(e:Event):void{
// log and/or show error?
if(!myTimer.running)
myTimer.start(); //start up again and hope for the best
}
function timerListener(e:TimerEvent):void {
// ADDED - stop timer and resume it after showXML (or error handler) has handled the response
myTimer.stop();
callPriceFeed();
}
function ShowPrice():void {
currentPriceTXT.text = "PRICE : "+myObj.currentPrice;
trace(gnutradeObj.currentPrice+" "+Math.random());
priceGlowMC.gotoAndPlay(2);
// ADDED - data from response ought to have migrated to the UI, now start up timer again
if(!myTimer.running)
myTimer.start();
}
精彩评论