Skip navigation
Currently Being Moderated

How to create a count up timer...

Feb 29, 2012 4:08 PM

This is the code I have for a count down timer in days, hours, minutes, and seconds but I would just like a timer that counts up from 0 until what ever number I would like and I would like to be able to control the pace....any ideas?

 

 

var targetDate:Date = new Date(2012,3,5,13);

 

addEventListener(Event.ENTER_FRAME, loop);

 

function loop(e:Event):void

{

    var nowDate:Date = new Date();

    var ms:Number = targetDate.getTime() - nowDate.getTime();

    var sec:Number = Math.floor(ms/1000);

    var min:Number = Math.floor(sec/60);

    var hr:Number = Math.floor(min/60);

    var day:Number = Math.floor(hr/24);

   

    sec = sec % 60;

    min = min % 60;

    hr = hr % 24;

   

    daytxt.text = day.toString();

    hrtxt.text = (hr < 10) ? "0" + hr.toString() : hr.toString();

    mintxt.text = (min < 10) ? "0" + min.toString() : min.toString();

    sectxt.text = (sec < 10) ? "0" + sec.toString() : sec.toString();

 
Replies
  • Currently Being Moderated
    Feb 29, 2012 4:28 PM   in reply to csclark0530

    Here's an extremely simple counter that will count up from 0 at a rate of one second. It just uses a Timer object.

     

    import flash.utils.Timer;

    import flash.events.TimerEvent;

     

    var upCounter:int = 0;

    var myTimer:Timer = new Timer(1000,0);

     

    myTimer.addEventListener("timer", timerHandler);

    myTimer.start();

     

    function timerHandler(event:TimerEvent):void

    {

              trace(upCounter);

              upCounter++;

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 29, 2012 6:29 PM   in reply to csclark0530

    Yes. If you want to have it start based on some event, then put the line "myTimer.start();" in a function.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 16, 2012 11:50 AM   in reply to Rob Dillon

    Hi,

    i'm trying to adapt this code for my count-up timer. i need a counter that ticks up every two seconds.

    i pasted the code below into a keyframe in flash. i then put a text field with a zero in it on the stage. when i test the movie, i see a flickering zero.

    what am i doing wrong?!

     

    thanks in advance,

    thomas

    ___________________________________________

     

    import flash.utils.Timer;

    import flash.events.TimerEvent;

     

    var upCounter:int = 0;

    var myTimer:Timer = new Timer(2000,7500);

    myTimer.addEventListener("timer", timerHandler);

    myTimer.start(1);

     

    function timerHandler(event:TimerEvent):void

    {

              trace(upCounter);

              upCounter++;

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 16, 2012 4:30 PM   in reply to tksinthecity2@yahoo.com

    change

    myTimer.start(1);

    to

    myTimer.start();

     

    The start function takes no arguments.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points