2 Replies Latest reply on Oct 4, 2006 6:41 AM by inlineblue

    Calculating elapsed time

    Vladimir Tsvetkov
      I'm trying to calculate the instantiation time of a given object. I try to pass this object to an Info MXML component, which is encapsulating the time calculation.

      This is the Info.mxml:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Button xmlns:mx=" http://www.adobe.com/2006/mxml"
      width="250" height="150"
      initialize="init();">

      <mx:Script>
      <![CDATA[
      import flash.utils.getTimer;

      [Bindable]
      public var object:Object;
      public var sTime:Number;

      private function init() : void
      {
      this.object.addEventListener("preinitialize", startTime, true);
      this.object.addEventListener("creationComplete", endTime, true);
      }

      private function startTime(e : Event):void
      {
      sTime = getTimer();
      }

      private function endTime(e : Event) : void
      {
      var eTime:Number = getTimer();

      var totalTime:Number = eTime - sTime;

      label = "Number of ms to create " + e.target + ":" + totalTime;
      }
      ]]>
      </mx:Script>

      </mx:Button>

      I use this component in the main application.mxml:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*"
      width="300" height="200">

      <local:Info object="{ application }" x="25" y="25"/>

      </mx:Application>

      I try to estimate the instantiation time of the application object, but somehow this object is not passed to the Info component. I've debugged this code in the builder - the object reference is null.

      What do I get wrong?

      Best Regards,
      Vladimir
        • 1. Calculating elapsed time
          Vladimir Tsvetkov Level 1
          It appears that the application object is still not initialized at the time I'm trying to pass it to the Info-component.
          If I try to call init() at creationCompleted instead of at initialize, then the object property is defined and is not null.
          But creationComplete is far to late if I want ot estimate instantiation time (actually I'm trying to calculate the initialization time) of the application object.
          In Flex 1.x there is the _global.startTime property - is there something analogous in Flex 2?
          • 2. Re: Calculating elapsed time
            inlineblue Level 1
            I think you're using too many high-level features (binding, visual components) for this to work. Before creationComplete, most features are not yet available. I think you're better off creating a pure AS class to do your timing and make use of the constructor:

            <mx:Application creationComplete="mytimer.endTime()">
            <mx:Script>
            <![CDATA[
            private var mytimer: Info = new Info(); //Constructor calls startTime()
            ]]>
            </mx:Script>
            </mx:Application>