A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Countdown Timer with Server Time AS3

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    1

    Countdown Timer with Server Time AS3

    Hello noob to flash cs4 with as3.

    I have a countdown script that pulls the users time, but I need the server time so visitors can't cheat the clock.

    Actionscript Code:
    function onLoop(evt:Event):void
    {
       
                   
       
        // Set the time variables
        // The present date
        var startTime   :Date   = new Date();  
        // The difference between the 2
        var difference  :Number = endTime.getTime() - startTime.getTime();
        // The number of seconds
        var secondsLeft :Number = Math.floor(difference     / 1000);
        // The minutes
        var minutesLeft :Number = Math.floor(secondsLeft    / 60);
        // The hours
        var hoursLeft   :Number = Math.floor(minutesLeft    / 60);
        // The days
        var daysLeft    :Number = Math.floor(hoursLeft      / 24);
        // Change the seconds, minutes and hours to real hours
        secondsLeft     %= 60;
        minutesLeft     %= 60;
        hoursLeft       %= 24;
        // Change the values to String
        var sec         :String = secondsLeft.toString();
        var min         :String = minutesLeft.toString();
        var hrs         :String = hoursLeft.toString();
        // With these values, check if the values are smaller then 2 and add an extra 0
        sec             = sec.length < 2 ? "0" + sec : sec;
        min             = min.length < 2 ? "0" + min : min;
        hrs             = hrs.length < 2 ? "0" + hrs : hrs;
        // Put the values in the textfields
        dayTxt.text     = String(daysLeft); // Days isnt a string yet
        hrsTxt.text     = hrs;
        minTxt.text     = min;
        secTxt.text     = sec;
        // Shadow text
        dayShTxt.text   = dayTxt.text;
        hrsShTxt.text   = hrsTxt.text;
        minShTxt.text   = minTxt.text;
        secShTxt.text   = secTxt.text;
        // Set the text color
        var parts:Array = ["day","hrs","min","sec"];
        for (var i:int = 0; i< 4; i++)
        {
            this[parts[i]+"Txt"].setTextFormat(fmt);
            this[parts[i]+"ShTxt"].setTextFormat(fmt);
            this[parts[i]+"Label"].setTextFormat(fmt);
            this[parts[i]+"ShLabel"].setTextFormat(fmt);
        }
       
        //if (dayTxt.text=="0" && hrsTxt.text=="00" && minTxt.text=="00" && secTxt.text=="00"){
           
            //var starterIn:Tween = new Tween(starter, "alpha", Strong.easeIn, 0, 1, 1, true);
            // Register the function as a listener with the button.
            //starter.addEventListener(MouseEvent.CLICK, starterGo);
           
        //}
       
        if (endTime < startTime){
            dayTxt.text="0";
            hrsTxt.text="00";
            minTxt.text="00";
            secTxt.text="00";
            dayShTxt.text="0";
            hrsShTxt.text="00";
            minShTxt.text="00";
            secShTxt.text="00";
            var starterInAfter:Tween = new Tween(starter, "alpha", Strong.easeIn, 1, 0, 1, true);
           
            // Register the function as a listener with the button.
            starter.addEventListener(MouseEvent.CLICK, starterGo);
        }
       
    }

    How do I get the server date and pass it to the date var startTime? I searched far and wide and can't find a simple solution. I know it has to be out there. Thank you for any help.

  2. #2
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    this is more complex than you think......look for "accurate time keeper" on the internet last time I looked http://www.computus.org/journal/?p=22 this isn't exactly a countdown timer but if you get it setup and get comfortable i'll tell you how to tweak it to countdown.
    ~calmchess~

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You need to have some sort of server-side script to generate the date, but all of these techniques can be defeated by a determined attacker.

    The simplest way is probably flashvars. Pass the server date in a flash var in the html code which embeds the swf. Then in the swf, retrieve it from root.loaderInfo.parameters.

    Second simplest, or about the same difficulty, would be external interface. Create a javascript function to return the server date (the javascript function will return a constant date which is parsed from server side generated content). Call that function from the swf.

    Third simplest is to create a server side page which returns the date in plain text, xml, or json, and use a URLLoader to load that content.

  4. #4
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    oops sorry i just read the title.
    ~calmchess~

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