# Actionscript 3 Second Counter (in Output)

**URL:** <https://forum.kirupa.com/t/actionscript-3-second-counter-in-output/264915>\
**Category:** flash\
**Created:** [July 2, 2008, 1:56am UTC](https://forum.kirupa.com/t/actionscript-3-second-counter-in-output/264915 "2008-07-02T01:56:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![basementsunrise](https://avatars.discourse-cdn.com/v4/letter/b/9fc29f/32.png) [@basementsunrise](https://forum.kirupa.com/u/basementsunrise)\
**Post date:** [July 2, 2008, 1:56am UTC](https://forum.kirupa.com/t/actionscript-3-second-counter-in-output/264915/1 "2008-07-02T01:56:41Z")

</div>

[COLOR=red]MORE HELP NEEDED DOWN BELOW! \<3[/COLOR]

Hello everyone, this is my first post on the Kirupa Forum, but won’t by my last. I’m new to scripting languages but as an aspiring graphic designer I am trying to learn flash. I’m sure this will all be very easy for most of you to fix.

Anyway, I’m trying to make a second counter that updates every second in the output window and I am pretty ■■■■ close but I can’t seem to get this to work! I need it to count up from 1 infinitely.

I made this model but it doesn’t work because the loop always resets the variable onTimer to 1 then add’s 1 making the output say, “2, 2, 2, 2, 2, 2, etc”

[COLOR=dimgray]var timer:Timer = new Timer(1000);[/COLOR]  
[COLOR=dimgray]timer.addEventListener(TimerEvent.TIMER, onTimer);[/COLOR]  
[COLOR=dimgray]timer.start();[/COLOR]

[COLOR=dimgray]function onTimer(e:TimerEvent):void [/COLOR]  
[COLOR=dimgray]{[/COLOR]  
[COLOR=dimgray]var now:Number = (1); now++; [/COLOR]  
[COLOR=dimgray]trace(now);[/COLOR]  
[COLOR=dimgray]}[/COLOR]

[COLOR=dimgray][COLOR=black]I know[/COLOR] [/COLOR]I need to create the now variable outside the onTimer because this is accessed every second. So I tried this but it doesn’t work and I’m starting to get really confused.

[COLOR=dimgray]var timer:Timer = new Timer(1000);[/COLOR]  
[COLOR=dimgray]timer.addEventListener(TimerEvent.TIMER, onTimer);[/COLOR]  
[COLOR=dimgray]timer.start();[/COLOR]

[COLOR=dimgray]function onTimer(e:TimerEvent):void [/COLOR]  
[COLOR=dimgray]{[/COLOR]  
[COLOR=dimgray]var then:Number = (0);[/COLOR]  
[COLOR=dimgray]then.addEventListener(TimerEvent.TIMER, addTo);[/COLOR]  
[COLOR=dimgray]}[/COLOR]

[COLOR=dimgray]function addTo(e:TimerEvent):void[/COLOR]  
[COLOR=dimgray]{[/COLOR]  
[COLOR=dimgray]var now:Number = then++;[/COLOR]  
[COLOR=dimgray]trace(now)[/COLOR]  
[COLOR=dimgray]}[/COLOR]

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![McGuffin](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mcguffin/32/408_2.png) [@McGuffin](https://forum.kirupa.com/u/McGuffin)\
**Post date:** [July 2, 2008, 2:12am UTC](https://forum.kirupa.com/t/actionscript-3-second-counter-in-output/264915/2 "2008-07-02T02:12:27Z")

</div>

You need to ask yourself what use it is resetting the now value to 1 every time the Timer event fires…

```auto

var timer:Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();

var now:int = 0;
function onTimer(e:TimerEvent):void 
{
    now++; 
    trace(now);
}

```

Notice it’s outside of the function call (and an integer, given it’ll only be displaying integer data).

---

<div class="post-metadata">

**Author:** ![basementsunrise](https://avatars.discourse-cdn.com/v4/letter/b/9fc29f/32.png) [@basementsunrise](https://forum.kirupa.com/u/basementsunrise)\
**Post date:** [July 2, 2008, 7:47am UTC](https://forum.kirupa.com/t/actionscript-3-second-counter-in-output/264915/3 "2008-07-02T07:47:07Z")

</div>

Oh I see!  
How wonderful thank you.  
I’m just really new to how the syntax reads out. Tought me a good lesson, thank you McGuffin.

---

<div class="post-metadata">

**Author:** ![basementsunrise](https://avatars.discourse-cdn.com/v4/letter/b/9fc29f/32.png) [@basementsunrise](https://forum.kirupa.com/u/basementsunrise)\
**Post date:** [July 8, 2008, 11:05am UTC](https://forum.kirupa.com/t/actionscript-3-second-counter-in-output/264915/4 "2008-07-08T11:05:47Z")

</div>

So I want to do the same thing only on Enter Frame instead of using a timer. I must just be missing something…

```auto
 
//stage.addEventListener(Event.ENTER_FRAME,onFrameLoop);
//
//var now:Number = (0);
//
//function onFrameLoop(evt:Event):void
//{
// (now++);
// if (now == (now %= 12))
// trace(now);
//}
 
stage.addEventListener(Event.ENTER_FRAME,onFrameLoop);
var now:Number = (a++);
var a:uint;
function onFrameLoop(evt:Event):void
{
(now++);
if (a == (now %= 12))
trace(a);
}

```

I’ve included two attempts, both with interesting outcomes.

---

<div class="post-metadata">

**Author:** ![yaim0310](https://avatars.discourse-cdn.com/v4/letter/y/85e7bf/32.png) [@yaim0310](https://forum.kirupa.com/u/yaim0310)\
**Post date:** [July 8, 2008, 4:32pm UTC](https://forum.kirupa.com/t/actionscript-3-second-counter-in-output/264915/5 "2008-07-08T16:32:24Z")

</div>

what are you trying to do with that?

on the first one you are making now increase by 1, but in the if statement, you are changing the value of now again with %= (thats like doing now = now % 12) and since now is that number its like checking now == now. so it will keep tracing now every frame.

on the second you are again changing now with the if but you are checking if a == now. somehow a equals 1 so when now cycles around to 1 it traces a which is 1. so about every 12 frames it traces 1;

---

<div class="post-metadata">

**Author:** ![basementsunrise](https://avatars.discourse-cdn.com/v4/letter/b/9fc29f/32.png) [@basementsunrise](https://forum.kirupa.com/u/basementsunrise)\
**Post date:** [July 8, 2008, 7:52pm UTC](https://forum.kirupa.com/t/actionscript-3-second-counter-in-output/264915/6 "2008-07-08T19:52:38Z")

</div>

I am trying to make the now variable add 1 every frame to the count and then every 12 time the count hits a number that is divisible by 12 it traces 1, 2, 3, 4, 5, 6, 7, 8 etc. Basically I want the numbers in the output to go up by 1 incrementally every second just like how we got it to work in this thread previously with a Timer, instead of Enter\_Frame.

Doesn’t the notation of " %= " mean divide? I don’t know I’m a little confused.

---

<div class="post-metadata">

**Author:** ![yaim0310](https://avatars.discourse-cdn.com/v4/letter/y/85e7bf/32.png) [@yaim0310](https://forum.kirupa.com/u/yaim0310)\
**Post date:** [July 8, 2008, 9:38pm UTC](https://forum.kirupa.com/t/actionscript-3-second-counter-in-output/264915/7 "2008-07-08T21:38:09Z")

</div>

% is modulo, it divides and returns the Remainder, meaning that ‘now % 12’ will return 0 is the number is divisible by 12. %= is different in that it will mod itself then assign itself the returned value, like i said before, now %= 12 is like using now = now % 12.

if you want stuff to happen it a certain time frame like a second i suggest use getTimer() which returns how long the program has been running in milliseconds.

```auto
var lastTime:Number = 0;
var currentTime:Number = 0;

stage.addEventListener(Event.ENTER_FRAME, onFrameLoop);
function onFrameLoop(e:Event) {
	
	currentTime = getTimer() / 1000 // turn into seconds
	
	if (currentTime - lastTime > 1) {
		lastTime = currentTime;
		
		// stuff every second here
	}
	
}

```
