User Guide
510 Using the External API
Back in the IMManager constructor, one of two things happens depending on the readiness of
the container. If
isContainerReady() returns true, the code simply calls the
setupCallbacks() method, which completes the process of setting up communication with
JavaScript. On the other hand, if
isContainerReady() returns false, the process is
essentially put on hold. A Timer object is created and is told to call the
timerHandler()
method every 100 milliseconds, as follows:
private function timerHandler(event:TimerEvent):void
{
// Check if the container is now ready.
var isReady:Boolean = isContainerReady();
if (isReady)
{
// If the container has become ready, we don't need to check anymore,
// so stop the timer.
Timer(event.target).stop();
// Set up the ActionScript methods that will be available to be
// called by the container.
setupCallbacks();
}
}
Each time the timerHandler() method gets called, it once again checks the result of the
isContainerReady() method. Once the container is initialized, that method returns true.
The code then stops the Timer and calls the
setupCallbacks() method to finish the process
of setting up communication with the browser.
Exposing ActionScript methods to JavaScript
As the previous example showed, once the code determines that the browser is ready, the
setupCallbacks() method is called. This method prepares ActionScript to receive calls from
JavaScript, as shown here:
private function setupCallbacks():void
{
// Register the SWF client functions with the container
ExternalInterface.addCallback("newMessage", newMessage);
ExternalInterface.addCallback("getStatus", getStatus);
// Notify the container that the SWF is ready to be called.
ExternalInterface.call("setSWFIsReady");
}