1 Reply Latest reply on Jan 5, 2011 5:45 AM by Ned Murphy

    [as2] getTimer()

    marcelozep3 Level 1

      Hi.

      I´m trying to catch when a second is passed using getTimer.

      I don´t want to use setInterval, because I´ll need to pause, and unpause it.

      So.. here´s my code:

       

      oldTime = getTimer();
      time = 0;
      newTime = 0;
      onEnterFrame = function ()
      {
      newTime = getTimer() - oldTime;
      time += newTime;
      oldTime = newTime
      if (time >= 1000)
      {
        trace("ok");
        time = 0;
       
      }
      };

       

      What is wrong with this code?

       

      It seems that the time variable reachs 1000 milliseconds too fast.

      I´m not getting a second interval.

       

      How would I fix that?

       

      Thanks

        • 1. Re: [as2] getTimer()
          Ned Murphy Adobe Community Professional & MVP

          Tf you trace the values you generate and compare in there you'll see you always have time > 1000 after it reaches it the first time.

           

          Try rethinking and de-complicating:

           

          var startTime = getTimer();
          var currentTime = startTime;
          onEnterFrame = function ()
          {
             currentTime = getTimer();


             if (currentTime - startTime >= 1000)
             {
                 trace("ok");
                 startTime = getTimer();
                 currentTime = startTime;
             }
          };