We could stop here, but why not make our ticker auto update? We know that the Yahoo stock data is updated every 20 minutes so let's make our stock ticker check how long its been running and update itself using that interval.
Luckily, Flash has included the getTimer() method, which allows us to get the time in milliseconds our application has been running. With this we can set the current time on the timer and set an interval equal to 20 minutes or (20 * 60000). Now using the ClipEvent( enterFrame ) call back function we can determine if the ticker has been running for 20 minutes and, if so, update the stocks. For those who don't know, the ClipEvent( enterFrame ) is constantly called during the life time of an Flash Application, making it a good place for constant data checks.
To implement the timer, make the following code changes to the stockTicker movie clip.
//sets interval for timer
onClipEvent( load )
{
var current = getTimer();
var interval = (20 * 60000); //every 10 minutes syncs stock data
//sets first set of stocks
this.removeAll();
_root.quoteArray.sort();
for( quotes in _root.quoteArray )
{
_root.GetQuote( _root.quoteArray[quotes] );
}
this.setDataAllWidth( 0 );
this.sortItemsBy( "text", "ASC" );
}
//checks to see if timer has elapsed
onClipEvent( enterFrame )
{
//time has elapsed
if( (current + interval) <= getTimer() )
{
current = getTimer();
//gets new data fro stocks
this.removeAll();
_root.quoteArray.sort();
for( quotes in _root.quoteArray )
{
_root.GetQuote( _root.quoteArray[quotes] );
}
this.setDataAllWidth( 0 );
this.sortItemsBy( "text", "ASC" );
}
}