A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Countdown timer - displaying milliseconds

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    2

    [RESOLVED] Countdown timer - displaying milliseconds

    Good Afternoon!

    Wasn't really sure where to put this, but A3 made the most sense, despite that fact I am a collossal newbie when it comes to Flash.

    What I'd like to do is create a countdown timer to a specific date, but from all the tutorials I've read, I can't seem to find one that will include milliseconds; only a Day:hour:min:sec format.

    What I'd like is an Hour:min:sec:milliseconds format (the milliseconds whilst considerably pointless, is an aesthetic choice - I'm hoping the frantically changing numbers will display a sense of urgency).

    Thanks in advance, any tricks or tips are greatly appreciated

    I'm using Flash CS4, and the following Actionscript so far:


    Actionscript Code:
    var endDate:Date = new Date(2012,9,6);

    var countdownTimer:Timer = new Timer(1000);
    countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);
    countdownTimer.start();

    function updateTime(e:TimerEvent):void
    {
        var now:Date = new Date();
        var timeLeft:Number = endDate.getTime() - now.getTime();
        var seconds:Number = Math.floor(timeLeft / 1000);
        var minutes:Number = Math.floor(seconds / 60);
        var hours:Number = Math.floor(minutes / 60);
        var days:Number = Math.floor(hours / 24);
       
        seconds %= 60;
        minutes %= 60;
        hours %= 24;
       
        var sec:String = seconds.toString();
        var min:String = minutes.toString();
        var hrs:String = hours.toString();
        var d:String = days.toString();
       
        if (sec.length < 2) {
            sec = "0" + sec;
        }
       
        if (min.length < 2) {
            min = "0" + min;
        }
       
        if (hrs.length < 2) {
            hrs = "0" + hrs;
        }
       
        var time:String = hrs + ":" + min + ":" + sec;
       
        time_txt.text = time;
    }
    Last edited by adamthestudent; 07-24-2010 at 01:34 PM. Reason: Problem Solved!

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The 1000 in new Timer(1000) tells it to trigger once every 1000 milliseconds. You could try reducing that, but I think trying every single millisecond will be too much. To make it as efficient as you can, use getTimer rather than creating a new Date in the function. Object creation is pretty expensive.

    If you do go with some value between 1 and 1000 for your timer interval and you want milliseconds displayed, you could try to use some unusual interval so that the thousandths place changes (if you use 100, you'll get 10ths of a second, but the 1000ths will remain at 0). Try something like 23, then the 1000ths place will change in an apparently random way, even though it's not actually random.

  3. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    2
    Thanks very much, that sorted the desired effect

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center