# \[FMX\] What is the most effecient way to do a timer?

**URL:** <https://forum.kirupa.com/t/fmx-what-is-the-most-effecient-way-to-do-a-timer/11806>\
**Category:** flash\
**Created:** [January 22, 2003, 4:56pm UTC](https://forum.kirupa.com/t/fmx-what-is-the-most-effecient-way-to-do-a-timer/11806 "2003-01-22T16:56:34Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![DJ\_Lex](https://avatars.discourse-cdn.com/v4/letter/d/f17d59/32.png) [@DJ\_Lex](https://forum.kirupa.com/u/DJ_Lex)\
**Post date:** [January 22, 2003, 4:56pm UTC](https://forum.kirupa.com/t/fmx-what-is-the-most-effecient-way-to-do-a-timer/11806/1 "2003-01-22T16:56:34Z")

</div>

I’m trying to figure out the most effecient way to do a timer.

Basically, I have the number of seconds for when the action should be done next.

Do I just loop 10 frames or so and check the getTimer or is there a better way to do it?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 22, 2003, 6:12pm UTC](https://forum.kirupa.com/t/fmx-what-is-the-most-effecient-way-to-do-a-timer/11806/2 "2003-01-22T18:12:42Z")

</div>

Actually, is it accurate to just have a movie that is running at 10fps, loops 10 frames, and has a time=time+1 script in it?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 22, 2003, 6:27pm UTC](https://forum.kirupa.com/t/fmx-what-is-the-most-effecient-way-to-do-a-timer/11806/3 "2003-01-22T18:27:24Z")

</div>

you should use setInterval. Its based on time and not frames so you could have your frame rate at anything and a setInterval timer wouldnt be affected. Plus, with setInterval, the timer operation would only happen when you need it to. In the case of a second-based timer, only once every second and not once every frame.

display.text = myTimer = 0;  
showTimer = function(){  
display.text = ++myTimer;  
}  
timerInterval = setInterval(showtimer,1000);

(btw ++myTimer is the same as saying myTimer = myTimer +1)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 23, 2003, 6:17pm UTC](https://forum.kirupa.com/t/fmx-what-is-the-most-effecient-way-to-do-a-timer/11806/4 "2003-01-23T18:17:24Z")

</div>

So here’s the thing. It’s more of a countdown. When the movie first runs, it gets the number of seconds and puts it into **trackRemain**.

I then show a countdown. When it gets to zero it needs to get a new number of seconds for **trackRemain**.

So I’m not quite sure how to use **setInterval** as a timer to compare it to trackRemain.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 23, 2003, 6:28pm UTC](https://forum.kirupa.com/t/fmx-what-is-the-most-effecient-way-to-do-a-timer/11806/5 "2003-01-23T18:28:57Z")

</div>

Ok - I think I get it.

I’m using the setInterval to subtract a second every second. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 24, 2003, 4:37pm UTC](https://forum.kirupa.com/t/fmx-what-is-the-most-effecient-way-to-do-a-timer/11806/6 "2003-01-24T16:37:19Z")

</div>

Is there a way to make this countdown from a specific number? For instance how would you make it countdown from 30 minutes? And is there a way to format the countdown so it looks like a clock (ie 30:00). thanks

David

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 24, 2003, 5:37pm UTC](https://forum.kirupa.com/t/fmx-what-is-the-most-effecient-way-to-do-a-timer/11806/7 "2003-01-24T17:37:03Z")

</div>

Bummer. Every time the setInterval gets reset, it goes wacky! I decided to just make a movie that is 10 frames long that loops, and make the fps 10. It’s accurate enough. 🙂

David,  
I just dealt with that issue. I’m sure there are several ways to do it, but here is something basic I wrote on my own. I’m new at FMX actionscripts so the code isn’t all that dynamic yet. Read through this explanation first and then look at the code.

**trackRemain** is the amount of time that my countdown starts with (in seconds). Set this value somewhere outside of your loop.

[font=courier]trackRemain = 1800[/font]

**MinuteRemain** converts the seconds into minutes (divides by 60) and then takes the integer of that result (rounds it to whole number). This will give you the number to put in the “00:” of your display.

**SecondRemain** takes the total number of seconds (from **trackRemain** ) and subtracts **MinuteRemain\*60** from it. This will give you the number to put in the “:00” of your display.

If you want your time to display with a leading zero (05:04 instead of 5:4), we need to check the **length** of your minutes and seconds. The code basically says, “if **MinuteRemain** is only one digit, then add a “0” to the front of it”.

I have a dynamic text field titled **AutoDisplay** that will display the results of the conversion and adds a “:” between them.  
[font=Courier]  
// Converts seconds to minutes and seconds  
MinuteRemain = int(trackRemain/60);  
SecondRemain = ((trackRemain) - (MinuteRemain\*60));  
// Adds a leading zero to Minutes  
if (length(MinuteRemain) == 1) {  
MinuteRemain = “0”+MinuteRemain;  
}  
// Adds a leading zero to Seconds  
if (length(SecondRemain) == 1) {  
SecondRemain = “0”+SecondRemain;  
}  
AutoDisplay = MinuteRemain + “:” + SecondRemain;  
[/font]  
At the end of my one second loop, I subtract a second from **trackRemain** and check to see it equals 0. If it does, I got to a frame outside of the loop called “countdownended”. If it does not equal 0, it goes to the beginning of the loop.  
[font=Courier]  
trackRemain = trackRemain-1;  
if (trackRemain = 0) {  
trackRemain = 0;  
autoDisplay = “00:00”;  
gotoAndPlay(countdownended);  
}  
gotoAndPlay(“startloop”);[/font]

Does that make sense? 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 24, 2003, 7:14pm UTC](https://forum.kirupa.com/t/fmx-what-is-the-most-effecient-way-to-do-a-timer/11806/8 "2003-01-24T19:14:13Z")

</div>

I finally got it to work. Here’s the final code. Thanks a million!

David

stop();  
trackRemain = 1800;  
function timercountdown(){  
MinuteRemain = int(trackRemain/60);  
SecondRemain = ((trackRemain) - (MinuteRemain\*60));  
if (length(MinuteRemain) == 1) {  
MinuteRemain = “0”+MinuteRemain;  
}  
if (length(SecondRemain) == 1) {  
SecondRemain = “0”+SecondRemain;  
}  
AutoDisplay = MinuteRemain + “:” + SecondRemain;  
if (trackRemain == “0”) {  
trackRemain = 0;  
autoDisplay = “00:00”;  
gotoAndPlay(“end”);  
}  
trackRemain = trackRemain - 1;  
}  
setInterval(timercountdown, 1000);

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 24, 2003, 7:27pm UTC](https://forum.kirupa.com/t/fmx-what-is-the-most-effecient-way-to-do-a-timer/11806/9 "2003-01-24T19:27:31Z")

</div>

Cool!

For some reason, setInterval was giving me a problem after my countdown reset. After it ran the first countdown with no problem, the second countdown would almost double in time, and the numbers would jump around.

If you wouldn’t mind, could you set your timer to 20 seconds and see if you get the same problem when it resets?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 24, 2003, 7:32pm UTC](https://forum.kirupa.com/t/fmx-what-is-the-most-effecient-way-to-do-a-timer/11806/10 "2003-01-24T19:32:20Z")

</div>

I understand what you mean by it jumping around weirdly if you rerun the script. I don’t ever have to rerun the script though. Once the timer hits zero the movie ends. How are you using your timer? If you have to jump around in a movie, place the script on a seperate level and extend the frames the entire length of the movie.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 24, 2003, 7:41pm UTC](https://forum.kirupa.com/t/fmx-what-is-the-most-effecient-way-to-do-a-timer/11806/11 "2003-01-24T19:41:07Z")

</div>

I’m getting my trackRemain seconds from an XML call. So the countdown is different each time it resets.

I’m writing a Flash based playlist window for my webcast. It displays info about the currently playing track.

The method I’m using of just looping one second worth of frames is working for me. I’ll post the completed project when it’s finished.

So, does anyone know why setInterval goes screwy?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 24, 2003, 9:29pm UTC](https://forum.kirupa.com/t/fmx-what-is-the-most-effecient-way-to-do-a-timer/11806/12 "2003-01-24T21:29:09Z")

</div>

Well… There is an easier and more efficient way than anything you guys said… Albeit… It is all about the frames per second… But you have to set that anyways!

```auto

framesPerSecond = 30;
countdownTimer = 0; 

// These two variables sets up a good system... Have
// this in your main onLoad thing.

// This next part have thrown in your onEnterFrame 
// commands.. 

if(countdownTimer > 0){countdownTimer--;}

// This will be thrown wherever you set up the 
// number of seconds you have in a statement
// or whenever you wanna make your timer start

countdownTimer = numberofSeconds * framesPerSecond;

```

Peace

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 24, 2003, 11:11pm UTC](https://forum.kirupa.com/t/fmx-what-is-the-most-effecient-way-to-do-a-timer/11806/13 "2003-01-24T23:11:10Z")

</div>

The majority of our code has to do with parsing the time in to the format “00:00”.

But we have several good ways to do the timer now. 🙂
