3 Replies Latest reply: Feb 26, 2009 12:10 PM by sneakyimp RSS

    getting time elapsed in AS3

    sneakyimp Community Member
      I have some asynchronous stuff going on in my Flash movie and would like to determine how much time elapses between two events -- e.g., a button click and the response from a socket server.

      I'm wondering how to go about this in AS3?
        • 1. Re: getting time elapsed in AS3
          chukcha14 Community Member
          var timer:Timer = new Timer(1000); //1000 milliseconds == 1 sec

          timer.addEventListener(TimerEvent.TIMER, incrementCounter);
          timer.start();

          function incrementCounter(evt:TimerEvent):void {
          this._someVariable++;
          }

          //as soon as the XMLSocket receives a response you would call
          timer.removeEventListener(TimerEvent.TIMER, incrementCounter);
          timer.stop();

          //and print out your counter
          trace(this._someVariable.toString());
          • 2. Re: getting time elapsed in AS3
            Some1Won Community Member
            You could make a date object and grab the time from there. Or a more simple way I think is you can use getTimer(). I believe it’s under flash.utils.getTimer().
            • 3. getting time elapsed in AS3
              sneakyimp Community Member
              Thanks for your response!

              I took your advice on using getTimer() and it's working swell. However, I was kind of hoping for something that didn't require my Flash movie to be running the whole time. I have learned that you can create a new Date object and access its time property and that will return a value in milliseconds. You can later create a new Date object, access its time property, and compare the two:

              var start:Date = new Date();
              trace('start:' + start.time);

              //calculate PI to a million digits or whatever

              var end:Date = new Date();
              trace('time elapsed:' + (end.time - start.time));

              I haven't tested that, but I think it'll work.