Skip to main content
Participating Frequently
March 1, 2012
Question

How to create a count up timer...

  • March 1, 2012
  • 1 reply
  • 5963 views

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();

This topic has been closed for replies.

1 reply

robdillon
Participating Frequently
March 1, 2012

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++;

}

Participating Frequently
March 1, 2012

if I upload that to a website will it count up continuously?

robdillon
Participating Frequently
March 1, 2012

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