A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: AS2: Countdown timer

  1. #1
    Junior Member
    Join Date
    Dec 2001
    Posts
    27

    AS2: Countdown timer

    I'm trying to create a simple countdown timer that countdowns in hrs/mins/secs from a defined number of hours in the future. (eg. count down from 3 hrs from now). Movie has a single frame with a dynamic text field with id "time_txt". I cobbled together the following from a couple of external sources, but i can't quite get it to work. Any guidance appreciated!

    Code:
    this.onEnterFrame = function()
    {
     var today:Date = new Date();
     var currentYear = today.getFullYear();
     var currentTime = today.getTime();
    
     var hoursAhead:Number = 3;
     var minsAhead:Number = hoursAhead*60;
     var targetDate:Date = new Date(today.getTime() + (minsAhead*60*1000));
     var targetTime = targetDate.getTime();
    
     var timeLeft = targetTime - currentTime; 
     var sec = Math.floor(timeLeft/1000);
     var min = Math.floor(sec/60);
     var hours = Math.floor(min/60);
     var days = Math.floor(hours/24);
     sec = String(sec % 60);
     if(sec.length < 2){
      sec = "0" + sec;
     }
     min = String(min % 60);
     if(min.length < 2){
      min = "0" + min;
     }
     hours = String(hours % 24);
     if(hours.length < 2){
      hours = "0" + hours;
     }
     days = String(days);
     if(timeLeft > 0 ){
      var counter:String = days + ":" + hours + ":" + min + ":" + sec;
      time_txt.text = counter;
     }else{
      trace("TIME'S UP");
            var newTime:String = "00:00:00:00";
            time_txt.text = newTime;
            delete (this.onEnterFrame);
     }
    }
    Thx! db
    doggBiter

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi

    Try messing around with this, or use this

    PHP Code:
    var hoursAhead:Number 3;

    var 
    getTargetDate:Date = new Date();
    var 
    targetTime:Number getTargetDate.getTime() + (hoursAhead 3600000);//600000 = 10 mins

    onEnterFrame = function()
    {
        var 
    today:Date = new Date();
        var 
    nowTime:Number today.getTime();
        var 
    overTime:Number targetTime nowTime;
        
        var 
    sec Math.floor(overTime 1000);
        var 
    min Math.floor(sec 60);
        var 
    hours Math.floor(min 60);
        var 
    days Math.floor(hours 24);
        
        
    sec String(sec 60);
        
        if (
    sec.length 2)
        {
            
    sec "0" sec;
        }
        
    min String(min 60);
        if (
    min.length 2)
        {
            
    min "0" min;
        }
        
    hours String(hours 24);
        if (
    hours.length 2)
        {
            
    hours "0" hours;
        }
        
    days String(days);

        if (
    overTime 0)
        {
            var 
    counter:String days ":" hours ":" min ":" sec;
            
    time_txt.text counter;
        }
        else
        {
            
    trace("TIME'S UP");
            var 
    newTime:String "00:00:00:00";
            
    time_txt.text newTime;
            
    delete onEnterFrame;
        }
    }; 

  3. #3
    Junior Member
    Join Date
    Dec 2001
    Posts
    27
    Quote Originally Posted by fruitbeard View Post
    Hi

    Try messing around with this, or use this

    PHP Code:
    var hoursAhead:Number 3;

    var 
    getTargetDate:Date = new Date();
    var 
    targetTime:Number getTargetDate.getTime() + (hoursAhead 3600000);//600000 = 10 mins

    onEnterFrame = function()
    {
        var 
    today:Date = new Date();
        var 
    nowTime:Number today.getTime();
        var 
    overTime:Number targetTime nowTime;
        
        var 
    sec Math.floor(overTime 1000);
        var 
    min Math.floor(sec 60);
        var 
    hours Math.floor(min 60);
        var 
    days Math.floor(hours 24);
        
        
    sec String(sec 60);
        
        if (
    sec.length 2)
        {
            
    sec "0" sec;
        }
        
    min String(min 60);
        if (
    min.length 2)
        {
            
    min "0" min;
        }
        
    hours String(hours 24);
        if (
    hours.length 2)
        {
            
    hours "0" hours;
        }
        
    days String(days);

        if (
    overTime 0)
        {
            var 
    counter:String days ":" hours ":" min ":" sec;
            
    time_txt.text counter;
        }
        else
        {
            
    trace("TIME'S UP");
            var 
    newTime:String "00:00:00:00";
            
    time_txt.text newTime;
            
    delete onEnterFrame;
        }
    }; 
    Thanks, Fruitbeard! Works like a charm!

    db
    doggBiter

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