Solution for a countdown timer in AS3?

Discussion in 'Web Design and Development' started by elephant, Feb 5, 2009.

  1. elephant macrumors newbie

    elephant

    Joined:
    Feb 5, 2009
    Location:
    London, UK
    #1
    I've used the tutorial and example from this link:

    http://schoolofflash.com/2008/04/tutorial-creating-a-timer-in-actionscript-3/

    ...to build a simple countdown timer for my website. However, I don't really understand AS3, and I think it would be a massive waste of time learning the entire langauge just to be able to do the one thing I need to do:

    The countdown is working as intended...BUT...it only counts down to a specific period in time.

    I'd like for it to either:

    1. Automatically start over every week, lets say tuesday (since this is Macrumors).

    or

    2. Count down to several specific anchor points. Lets say that it should count down to March 30th, and when it reaches zero, it starts counting down to April 14th.

    The second behaviour would fit my needs perfectly, but the first behaviour is good enough.

    Anyone care to teach me how to accomplish this?

    Thanks. :)
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    Well, I don't know ActionScript, but here's the logic for at least one approach. You currently have a variable for the current end date for the count down. Instead of a single date variable, create an array of date variables. Then you'll loop through each date, and pick the first date that has yet to occur (that assumes dates are in chronological order). Then you'll feed that date into the existing countdown script.

    Pseudo code:
    PHP:
    var dates = new Array(
      new 
    Date(2008,03,30),
      new 
    Date(2008,04,14)
    );
    var 
    endDate dates[0]; // give it something in case all dates have passed
    var today = new Date();
    for (var 
    a=0dates.lengtha++) {
      if (
    today dates[a]) {
        
    endDate dates[a];
        break;
      }
    }
    Again, that's pseudo code, which means you can't just copy and paste it. When someone who knows ActionScript comes through perhaps they'll improve on this code.
     
  3. lucidmedia macrumors 6502a

    Joined:
    Oct 13, 2008
    Location:
    Wellington, New Zealand
    #3
    if you are not comfortable with AS3, am i right in thinking that the rest of your site is not in flash?

    If so, you can find a lot of well-documented PHP countdown timers that are quite easy to configure.

    If you are going the AS3 route, the tutorial has essentially already shown you how to add or subtract "time" using the date class.

    Another option is to use the parse() method of the as3 date class. This will convert a date into unix time (milliseconds since Jan. 1 1970). You may find it easier to add or remove time from a date in this format. For example, you can add the number of milliseconds in a week to the current date (in unix format) to create a new end point for your timer.
     
  4. elephant thread starter macrumors newbie

    elephant

    Joined:
    Feb 5, 2009
    Location:
    London, UK
    #4
    Correct, the only flash element on the website is that countdown timer. Because of basically one reason only: it has a rare custom font, and I'm using flash to embed it.

    If the tutorial shows how to do what I want to achieve, I'll have to take another (closer) look...however, for someone with no knowledge at all regarding AS3, it's basically like reading chinese.

    I understand how I change the date, and I have no problem setting any date I want. It's just the repitition part that I need to learn.
     
  5. elephant thread starter macrumors newbie

    elephant

    Joined:
    Feb 5, 2009
    Location:
    London, UK
    #5
    In another tutorial, I found this comment:

    Would my problem be easily solved by making a new counter (with a new end date) in frame 2, and then another in frame 3 (etc) perhaps?

    The above code doesn't work though, but maybe someone could tell me an appropriate code to go to frame 2 when the timer reaches zero, and then I can solve the rest myself. :)
     
  6. lucidmedia macrumors 6502a

    Joined:
    Oct 13, 2008
    Location:
    Wellington, New Zealand
    #6
    You don't need frames for a dynamic animation like this... you want to use a timer, as the original tutorial did.

    A timer is an As3 object that runs a certain chunk of code at regular intervals.

    That way, your final movie will have only one frame, and the code triggered by the timer event will update the text field with you embedded font.
     
  7. elephant thread starter macrumors newbie

    elephant

    Joined:
    Feb 5, 2009
    Location:
    London, UK
    #7
    Alright, and is there an easy way to explain how I do this? I feel like I'm almost there, as my countdown timer works, and I even managed to get it to stop and display a text when it reaches zero. :cool:
     
  8. elephant thread starter macrumors newbie

    elephant

    Joined:
    Feb 5, 2009
    Location:
    London, UK
  9. matius macrumors newbie

    Joined:
    Feb 16, 2009
    #9
    Have you found a solution? I'm working on this very problem at the moment, except, it deals with random, rather than weekly events.

    There's some work that goes into it. If I find something I'll post.

    M
     
  10. L3EOO macrumors newbie

    Joined:
    Feb 24, 2009
    #10
    I have recently created a countdown timer for an event we are having, how did you get text to display once the timer reached zero???
     

Share This Page