# Upcount timer

**URL:** <https://forum.kirupa.com/t/upcount-timer/28715>\
**Category:** flash\
**Created:** [August 17, 2003, 3:01pm UTC](https://forum.kirupa.com/t/upcount-timer/28715 "2003-08-17T15:01:13Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Voetsjoeba](https://avatars.discourse-cdn.com/v4/letter/v/ac91a4/32.png) [@Voetsjoeba](https://forum.kirupa.com/u/Voetsjoeba)\
**Post date:** [August 17, 2003, 3:01pm UTC](https://forum.kirupa.com/t/upcount-timer/28715/1 "2003-08-17T15:01:13Z")

</div>

Hi,

I want to create a timer that counts up. I’ve searched but all I found were countdown timers. Any ideas on how to do this ?

---

<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:** [August 17, 2003, 3:15pm UTC](https://forum.kirupa.com/t/upcount-timer/28715/2 "2003-08-17T15:15:06Z")

</div>

```auto
iniTime = getTimer();
onEnterFrame = function(){
msecs = getTimer()-iniTime;
// convert if necessary from milliseconds (msecs) to anything else
};

```

---

<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:** [August 17, 2003, 3:19pm UTC](https://forum.kirupa.com/t/upcount-timer/28715/3 "2003-08-17T15:19:48Z")

</div>

Sorry, my mistake. I didn’t explain well: I’m using it for an mp3 player, so I have to be able to stop, pause, and reset it. Which I don’t think is possible with getTimer();

Thanks for helping though sen 🙂

---

<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:** [August 17, 2003, 3:36pm UTC](https://forum.kirupa.com/t/upcount-timer/28715/4 "2003-08-17T15:36:00Z")

</div>

getTimer is just used to get a change in time. In the above code, iniTime is used to get the initial getTimer timer and then its assigned to msecs. If you need to stop and restart it, you just need to reset iniTime and add the new calculated msecs to a general timer variable.

```auto

RunTimer = function(){
theTime += getTimer()-iniTime;
// convert if necessary from milliseconds (msecs) to anything else
};
StartTimer = function(){
iniTime = getTimer();
this.onEnterFrame = RunTimer;
};
StopTimer = function(){
delete this.onEnterFrame;
};

theTime = 0;
iniTime = getTimer();
StartTimer();

```

---

<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:** [August 17, 2003, 6:46pm UTC](https://forum.kirupa.com/t/upcount-timer/28715/5 "2003-08-17T18:46:00Z")

</div>

Hey Sen, please explain:

I placed this at the beginning of the main timeline:  
[AS]  
RunTimer = function(){  
theTime = Math.round((getTimer()-iniTime)/1000)  
};  
StartTimer = function(){  
iniTime = getTimer();  
this.onEnterFrame = RunTimer;  
};  
StopTimer = function(){  
delete this.onEnterFrame;  
};

theTime = iniTime = getTimer();  
StartTimer();  
[/AS]

And then used StopTimer(); to stop the timer. It worked, but I don’t understand the first part, getTimer()-iniTime. iniTime is a timer too, so you’re substracting a timer from a timer ? And theTime is a timer too, so you’re actually substracting iniTime from a certain getTimer and store it in theTime.

Now when I place only getTimer()-iniTime in a loop and trace the result, it’s 0,0,0,0,2,0,0,1,0,0,0,3,0,0,0,5, … That’s because you are substracting two timers which started practically at the same time right ? But then why does the whole timersystem work ?

I also modified your code a little bit ( theTime += getTimer()-iniTime was supposed to be theTime = Math.round((getTimer()-iniTime)/1000) ).

Ok, I get all the rest except what I explained before. It worked to reset the timer (whichs equals stop on the mp3 control), but how can I pause and resume the timer ?

---

<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:** [August 18, 2003, 11:41am UTC](https://forum.kirupa.com/t/upcount-timer/28715/6 "2003-08-18T11:41:43Z")

</div>

Ok, I got everything working the way I want it to, but I can’t figure out how to pause/resume the timer. As I said before, I’m using it for an MP3 player. You can see it [here](http://voetsjoeba.asianillusion.com/player.html). I can provide the FLA too if you want it.

This is the structure of my FLA:

I use an external swf with data for the equalizer. At the first frame of the main timeline, I put these actions:  
[AS]  
loadMovie(“ldg.swf”, “\_level1000”);[//ldg.swf](https://ldg.swf) is the datafile  
paused = false;  
minutes = 0;  
RunTimer = function () {  
theTime = Math.floor((getTimer()-iniTime)/1000);  
if (theTime\<10) {  
theTime = “0”+theTime;  
}  
if (theTime\>=10) {  
theTime = “”+theTime;  
}  
if (theTime\>59) {  
NewTimer();  
}  
\_root.timerText = “0”+minutes+":"+theTime;  
};  
StartTimer = function () {  
iniTime = getTimer();  
this.onEnterFrame = RunTimer;  
};  
StopTimer = function () {  
delete this.onEnterFrame;  
minutes = 0;  
\_root.timerText = “00:00”;  
};  
PauseTimer = function (){  
currentM = minutes  
currentT = theTime  
delete this.onEnterFrame  
}  
NewTimer = function () {  
delete this.onEnterFrame;  
minutes++;  
StartTimer();  
};  
theTime = iniTime=getTimer();[/AS]

Ok, now the two last frames of my player are a loop, because the equalizer needs to change constantly. So the last frame has [AS]gotoAndPlay(‘Spectrum’);[/AS]  
and the frame right before it has the label Spectrum. That frame (Spectrum) has these actions:

[AS]  
for (i=0; i\<18; i++) {  
this[“l”+i].\_height = 120-(110-\_level1000[“s”+i]_1.2);  
}  
\_root.progress.\_x = 12.8+((161_\_level1000.\_currentframe)/\_level1000.\_totalframes);[/AS]

l1, l2, l3, … are equalizer bars and \_root.progress is a bar representing the playhead. These all work fine. Now for the playback buttons:

Stop -  
[AS]  
on(release){  
\_level1000.gotoAndStop(1);  
\_root.playing = false;  
StopTimer();  
}  
[/AS]

Play -  
[AS]  
on (release) {  
if (\_root.playing == false) {  
\_level1000.play();  
StartTimer();  
\_root.playing = true;  
}  
}  
[/AS]

Pause (so far) -  
[AS]  
on (release) {  
if (paused == false) {  
\_level1000.stop();  
PauseTimer();  
\_root.playing = false;  
paused = true;  
} else if (paused == true) {  
\_level1000.play();  
ResumeTimer();  
\_root.playing = true;  
paused = false;  
}  
}  
[/AS]  
But I don’t know how to pause/resume the timer so I can’t put action on the pause button. The song itself pauses and resumes perfectly, it’s just the timer. For the notice; the pause buttons pauses when clicks and resumes when clicked again, the stop button stops and rewinds, and the play button plays from the beginning (it is used with the stop button), but should also be able to resume pause. Can you help me Sen ?

---

<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:** [August 18, 2003, 1:02pm UTC](https://forum.kirupa.com/t/upcount-timer/28715/7 "2003-08-18T13:02:44Z")

</div>

Can i see the fla?

---

<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:** [August 18, 2003, 1:07pm UTC](https://forum.kirupa.com/t/upcount-timer/28715/8 "2003-08-18T13:07:10Z")

</div>

… no 😛 Just kidding, of course you can. But you’ll need the data swf, which is quite big (3-4MB) for it to work, otherwise you won’t be able to do much. Do you have MSN Messenger ?

---

<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:** [August 18, 2003, 1:08pm UTC](https://forum.kirupa.com/t/upcount-timer/28715/9 "2003-08-18T13:08:12Z")

</div>

Yup  
\*\*\*\*\*@hotmail.com

---

<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:** [August 18, 2003, 1:09pm UTC](https://forum.kirupa.com/t/upcount-timer/28715/10 "2003-08-18T13:09:55Z")

</div>

Great, I’m there in a sec.

---

<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:** [August 19, 2003, 4:14am UTC](https://forum.kirupa.com/t/upcount-timer/28715/11 "2003-08-19T04:14:14Z")

</div>

Sorry Voetsjoeba, i was busy all day and couldnt look into your file earlier.  
I think its working now.

---

<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:** [August 19, 2003, 5:01am UTC](https://forum.kirupa.com/t/upcount-timer/28715/12 "2003-08-19T05:01:14Z")

</div>

Man, you shouldn’t be apologizing at all ! The FLA you gave me works perfectly ! Thanks so much =) !

---

<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:** [August 19, 2003, 5:03am UTC](https://forum.kirupa.com/t/upcount-timer/28715/13 "2003-08-19T05:03:10Z")

</div>

Glad i could help Voetsjoeba  
=)

---

<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:** [August 19, 2003, 10:22am UTC](https://forum.kirupa.com/t/upcount-timer/28715/14 "2003-08-19T10:22:09Z")

</div>

Hey claudio, I’ve got a few more questions for you. I modified the fixed FLA you gave me, I placed the \_root.onEnterFrame actions from the last frame inside the frame 6-7 loop, and the functions that were defined on the last frame, I put those at the fifth frame, because I don’t want too much enterFrames so I’m placing it all in one loop and only defining those functions once. It still works fine.

This player is supposed to have multiple songs. So I created an array at the first frame with objects in it to define the filename, title and artist of each data file. This is how my first frame looks like now:

[AS]  
song = new Array();  
song[0] = {filename:“ldg”,artist:“Rob Zombie”,title:“Living Dead Girl”}  
song[1] = {filename:“dragula”,artist:“Rob Zombie”,title:“Dragula”}  
song[2] = {filename:“03”,artist:“Can’t remember”,title:“Can’t remember either :P”}  
function loadSong(arraynr){  
var temp = song[arraynr].filename+".swf";  
artist.text = song[arraynr].artist  
title.text = song[arraynr].title  
loadMovie(temp, “\_level1000”)  
\_level1000.gotoAndStop(1);  
}  
\_root.songnr = 0;  
loadSong(\_root.songnr)  
[/AS]

… and, the 6-7 loop is now:

[AS]  
for (i=0; i\<18; i++) {  
this[“l”+i].\_height = 120-(110-\_level1000[“s”+i]_1.2);  
}  
\_root.progress.\_x = 12.8+((161_\_level1000.\_currentframe)/\_level1000.\_totalframes);  
theTime = getTimer()/1000-iniTime;  
goTime = theTime-buttonPressTime;  
if (playing) {  
minutes = Math.floor((goTime/3600)\*60);  
seconds = Math.floor(((goTime/3600)\*60-minutes)\*60);  
myseconds = (seconds\<10) ? “0”+seconds : seconds;  
myminutes = (minutes\<10) ? “0”+minutes : minutes;  
timerText = myminutes+":"+myseconds;  
}  
[/AS]

The onEnterFrame part of the script at the last frame was moved to the 6-7 loop, as said before, and the button functions were moved to the fifth frame. So my fifth frame looks like this:  
[AS]  
function stopTimer() {  
timerText = “00:00”;  
buttonPressTime = getTimer()/1000-iniTime;  
\_level1000.gotoAndStop(1);  
pause();  
}  
function pause() {  
\_level1000.stop();  
pauseTime = getTimer()/1000;  
playing = false;  
}  
function unpause() {  
unpauseTime = getTimer()/1000;  
iniTime += (unpauseTime-pauseTime);  
\_level1000.play();  
playing = true;  
}  
playbutton.onPress = function() {  
if (!playing) {  
unpause();  
pausebutton.gotoAndStop(1);  
}  
};  
stopbutton.onPress = function() {  
stopTimer();  
};  
previous.onPress = function(){  
\_root.songnr–;  
if (\_root.songnr \< 0){  
\_root.songnr = song.length-1;  
}  
stopTimer();  
loadSong(\_root.songnr)  
gotoAndPlay(3)  
};  
\_root.playing = true;  
\_level1000.play()  
[/AS]

Now when I click the previous button, there is a lag, and then it starts playing the previous song (which is correct), but the timer starts at 00:04. I’ve tried everything but I can’t get that timer right. Everythin seems correct, but still it doesn’t work.

And why does it lag ? gotoAndPlay(3) is the frame where the loading indication is. I tried replacing the code from frame 3 with a preloader, but no luck.

---

<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:** [August 19, 2003, 10:28am UTC](https://forum.kirupa.com/t/upcount-timer/28715/15 "2003-08-19T10:28:06Z")

</div>

I found it: when the data file isn’t loaded yet, it lags, but the timer already starts. When it is already loaded, it starts playing immediately which makes the timer correct.

So I’d have to load all the data files at the beginning ? No, there’s got to be a better way. There should be either a preloader at frame 3, or a loading indication at frame, not a lag.

---

<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:** [August 19, 2003, 3:57pm UTC](https://forum.kirupa.com/t/upcount-timer/28715/16 "2003-08-19T15:57:49Z")

</div>

For some reason, the lag was gone when I added pausebutton.gotoAndStop(1). But when the previous button has been clicked several times, the equalizer became framy. I noticed that, when a framy equalizer is playing, that when you click play and stop, it becomes smooth again. So I added those two actions (from the play and stop buttons) to the previous button. So that’s why I have this script now:

[AS]function stopTimer() {  
timerText = “00:00”;  
buttonPressTime = getTimer()/1000-iniTime;  
\_level1000.gotoAndStop(1);  
pause();  
}  
function pause() {  
\_level1000.stop();  
pauseTime = getTimer()/1000;  
playing = false;  
}  
function unpause() {  
unpauseTime = getTimer()/1000;  
iniTime += (unpauseTime-pauseTime);  
\_level1000.play();  
playing = true;  
}  
playbutton.onPress = function() {  
if (!playing) {  
unpause();  
pausebutton.gotoAndStop(1);  
}  
};  
stopbutton.onPress = function() {  
stopTimer();  
};  
previous.onPress = function() {  
\_root.songnr–;  
if (\_root.songnr\<0) {  
\_root.songnr = song.length-1;  
}  
pausebutton.gotoAndStop(1);  
loadSong(\_root.songnr);  
gotoAndPlay(3);  
stopTimer();  
delete (buttonPressTime);  
delete (pauseTime);  
unpause();  
delete (unpauseTime);  
};  
\_root.playing = true;  
\_level1000.play();[/AS]

Now the framyness doesn’t show up for the first three or four times, but after that, it still gets framy. And, the timer is messed up when I click the previous button. Any ideas how to fix this ?

---

<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:** [August 19, 2003, 4:31pm UTC](https://forum.kirupa.com/t/upcount-timer/28715/17 "2003-08-19T16:31:22Z")

</div>

Hmm i dunno why thats happening voetsjoeba… i will try to look over your file again.
