Search code examples
actionscript-3flashapache-flexairflex4.5

How to get the time which is spend for loading the flash object?


  • how to get the time which tooks the flash object to load inside the browser, from the creation of the object untill it's ready state ?

Solution

  • If you just want to measure the loading time of the Flash object, you can just use FireBug. But if you want to know when a Flex application is completely ready for usage (i.e. all RSL's loaded; all initial data loaded), I think there are two approaches. One within the app, using a custom preloader to start timing and stop timing when your conditions are met. The other through JavaScript and ExternalInterface. Of which I reckon the latter will give you the most accurate result, because there will probably already be a delay before the preloader is loaded.

    JavaScript and ExternalInterface

    I have never done this so I'm just going to explain my thoughts. In Javascript you create a 'date' object when the Flash object starts loading. You're probably using SWFObject to inject the Flash object into the page, so you can probably hook up somewhere in there. Then inside your Flex application, when the necessary conditions are met (you define what those are), you use the ExternalInterface.call() to tell JavaScript that the Flex app is ready. Make a new 'date' and subtract the first 'date', and you have your loading time.

    More info on using ExternalInterface can be found in the docs: http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7cb2.html

    more detail

    Create the JavaScript function that we'll call when the app is ready:

    function onFlexAppReady() {
        var end = new Date().getTime();
        var loadTime = end - start; //in ms.
    }
    

    Now in the code that was generated by FlashBuilder, add the start time before the swf is injected:

    var start = new Date().getTime();
    swfobject.embedSWF(
                "MyFlexApp.swf", "flashContent", 
                "100%", "100%", 
                swfVersionStr, xiSwfUrlStr, 
                flashvars, params, attributes);
    

    Lastly, in your Flex app, when the right conditions are met, call the JavaScript function:

    ExternalInterface.call("onFlexAppReady");
    

    Again, this is all untested code, but it should get you started.