A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: How can I make this function loop?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2

    How can I make this function loop?

    So i am trying to build a countdown timer (for building a fitness app).

    Here is my code for the very basic timer. I'm trying to use getTimer() because the timer class is not accurate at all. I'm trying to start a looping function with a button. And I'm able to make it fire once, but not repeat. This code was modified from something I found online, so if you think I'm totally in the wrong direction please let me know.

    Thanks

    Code:
    var countDownTime:Number = 10;
    var startTimeUTC:Number = getTimer() / 1000;
    trace(startTimeUTC)
    timeText.text = String(countDownTime);
    btn1.addEventListener(MouseEvent.CLICK, startTimer);
    
    function startTimer(event:MouseEvent):void
    {
    	var secondsUTC:Number = getTimer() / 1000;
    	var remainingTime:Number = ((startTimeUTC + countDownTime) - secondsUTC); 
    	
    	if (remainingTime > 0)
    	{
    	timeText.text = String(Math.round(remainingTime));
    	}
    	else
    	{
    	timeText.text = "0"
    	}
    	
    }

  2. #2
    Senior Member hts5000's Avatar
    Join Date
    Oct 2007
    Posts
    160
    Try this:

    Actionscript Code:
    var countDownTime:Number = 10;
    var startTimeUTC:Number = getTimer()/1000;
    trace(startTimeUTC);
    timeText.text = String(countDownTime);
    btn1.onPress = function() {
        var secondsUTC:Number = getTimer()/1000;
        this.onEnterFrame = function() {
            var remainingTime:Number = ((startTimeUTC+countDownTime)-secondsUTC);
            if (remainingTime>0) {
                timeText.text = String(Math.round(remainingTime));
            } else {
                delete this.onEnterFrame;
                timeText.text = "0";
            }
        };
    };

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    ahhhh yes i knew i had to put the enterframe listener in there somewhere. thanks!!!!

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