Best viewed in Firefox

Awesty Productions

Making A Timer With setInterval();

January 30th, 2007 by awesty

In this tutorial you will learn how to make an easy timer with the setInterval() function. You should have a basic knowledge of actionscript, but other than that this tutorial is pretty short.

Here is an example of what we will be making:

First of all, create a dynamic text box and give it a variable name of ‘timer’. Now all you have to do is put this code on the frame.

_root.timer = 100; //Makes the Timer equal 100. Change it to whatever number you want.
clearInterval(id); //If this wasnt here, after the first second the function would run everyframe
id = setInterval(function () { //Read below for the rest…
    _root.timer–; //Add another hyphon onto the end
}, 1000);

I only explained half the code there. This is how the setInterval function works. You give it an ID (in this case ‘id’). It has 3 parameters, we only used 2 though. The first one is a function. In this case the function make the timer decrease by one. The next parameter is a time, in milliseconds. We put 1000 (1 second), so the timer decreases by 1 every second.

ID = setInterval(Function,Interval,Params);

Thats it! An easy to make time ;)

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • del.icio.us
  • digg
  • Furl
  • MyShare
  • NewsVine
  • Netscape
  • Reddit
  • Simpy
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

RSS feed

107 Comments »

Comment by KIM
2007-01-30 18:52:52

nice!

 
Comment by awesty
2007-01-30 18:55:45

Thanks. ;D

 
Comment by KIM
2007-01-30 18:57:11

but it goes to negative numbers…
is this how to solve it?

if(_root.timer

 
Comment by KIM
2007-01-30 18:58:24

oopsy

it should go

if(_root.timer (LESSTHAN) 0){
_root.timer = 0;
}

 
Comment by KIM
2007-01-30 19:03:43

and theres also a prob… it says…

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 5: Syntax error.
_root.timer–;

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 6: ‘)’ or ‘,’ expected
}, 1000);

Total ActionScript Errors: 2 Reported Errors: 2

pls help… :)

Comment by Horhey Subscribed to comments via email
2008-01-10 12:26:57

Yeah I got the same error, for the second one. **Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 6: ‘)’ or ‘,’ expected
}, 1000);

Comment by awesty
2008-01-10 14:22:21

But there are only 5 lines of code…

Comment by Jacob C Greenleaf Subscribed to comments via email
2008-05-22 11:45:07

Yeah i get

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 4: Syntax error.
_root.timer–-; //Add another hyphon onto the end

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 5: ‘)’ or ‘,’ expected
}, 1000);

Total ActionScript Errors: 2 Reported Errors: 2

 
 
 
 
Comment by awesty
2007-01-30 19:14:47

You need to add an extra hyphen onto the end of the _root.timer-;

Also just put:
Math.Max(_root.timer,0);

 
Comment by KIM
2007-01-30 20:04:16

tnx

 
Comment by KIM
2007-01-30 20:12:49

there’s still

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 5: Syntax error.
_root.timer–;

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 6: ‘)’ or ‘,’ expected
}, 1000);

Total ActionScript Errors: 2 Reported Errors: 2

 
Comment by Bob
2007-01-30 22:04:19

Nice. Exactly what I was looking for.

If I added

if (_root.timer=0) {
gotoAndPlay(2)
}

To the timer would I be able to go to the next frame when the timer reached zero?

 
Comment by big al
2007-01-31 10:39:48

cough cough…. awesty, your homepage timer is bellow -3000 now… might wana update

 
Comment by awesty
2007-01-31 15:29:49

@KIM: Read my last comment.

@Bob: You need two ‘=’ signs. _root.timer == 0

@Big al: It is only local to your computer. So if you refreshed the page it would go back up to 100.

 
Comment by KIM
2007-01-31 18:37:44

whats a hyphen?

 
Comment by KIM
2007-01-31 18:38:59

the minus thingy?

 
Comment by abhilash
2007-01-31 18:47:28

nice
soon i will e-mail one or two games which i have made.But what’s ur e-mail?
My 2 will be -
1. 2player car racing
2.A simple yet addictive cricket game

 
Comment by awesty
2007-01-31 21:32:07

@KIM: Yes.

@abhilbash:

awestyproductions[@]hotmail[.]com

 
Comment by Bob
2007-02-01 06:53:34

Nope, still doesn’t work. This is the code I used:

stop();
_root.timer = 5;
clearInterval(id);
id = setInterval(function () {
_root.timer–;
}, 1000);
if (_root.timer==0) {
gotoAndPlay(2)
}

 
Comment by awesty
2007-02-01 21:04:46

I dont think you can have the stop there, try removing that.

 
Comment by abhilash
2007-02-02 21:52:29

sorry but i’ll be not able to e-mail those games till march because my exams are there and i’ll be not able to complete those games.

 
Comment by awesty
2007-02-03 11:02:41

kk, it doesnt worry me

 
Comment by KIM
2007-02-04 18:18:29

AHH!!!!

i can’t belive im saying this but i give up on this freekin timer!

 
Comment by onyx
2007-02-05 15:35:34

lets say I was making a “breakout” type game… (you know the one w/ the blocks and the ball that bounces off of a controlable paddle and breaks the blocks)… my game works fine but I want to add a timer for a time bonus to add to your score… would I use this timer or something else?

 
Comment by awesty
2007-02-05 19:36:55

@KIM: Try just typing out the code, no copying/pasting, not including the comments and see if that works.

@onyx: Well say you finished the level and the timer still had 100 seconds left on it, you could just do something like:

score += timer;

So if score was 54, it would then equal 154. ;)

 
Comment by KIM
2007-02-05 19:42:27

hmm…………………………………………………………………………..

 
Comment by Bob
2007-02-09 06:57:58

Is there any way to make the timer stop? Maybe you could make a part two tutorial on some different things you can do with your timer.

 
Comment by awesty
2007-02-09 16:36:52

you could do that with an if else statement, like:

If(timer is less then or equal to 0){
do something
}

 
Comment by Kurt Subscribed to comments via email
2007-02-11 05:40:30

I’m confused i can’t put the code in??? i don’t under stand well i don’t think I’m doing it right but i use the text option thing then i type text make dynamic then erase the text i think that is really wrong but thats the only way i know how to get dynamic text.

 
Comment by awesty
2007-02-11 12:19:41

You just have to make a normal text box, select it, open the properties panel and then select dynamic from the drop down menu. Then give it the variable name etc.

 
Comment by random
2007-02-12 00:52:18

Lol, when it reaches 0, it gets to -1, -2, -3 and so on xD

 
Comment by awesty
2007-02-12 19:29:18

You can fix that with a simple if statement ;)

 
Comment by chelion
2007-02-15 03:53:13

This is what I have in the action script for the frame:

_root.timer = 100;
clearInterval(id);
id = setInterval(function () { ID = setInterval(Function,Interval,Params);
_root.timer–;-
}, 1000);

and this is my error message:

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 4: Syntax error.
_root.timer–;-

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 5: Operator ‘-’ must be followed by an operand
}, 1000);

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 3: Statement block must be terminated by ‘}’
id = setInterval(function () { ID = setInterval(Function,Interval,Params);

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 5: Syntax error.
}, 1000);

Total ActionScript Errors: 4 Reported Errors: 4

 
Comment by chelion
2007-02-15 03:56:36

i just got rid of one error this is my error message now:

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 4: Syntax error.
_root.timer–; - }, 1000);

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 3: Statement block must be terminated by ‘}’
id = setInterval(function () { ID = setInterval(Function,Interval,Params);

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 4: Syntax error.
_root.timer–; - }, 1000);

Total ActionScript Errors: 3 Reported Errors: 3

 
Comment by chelion
2007-02-15 04:03:05

sorry but i got rid of another one:

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 4: Syntax error.
_root.timer–;- }, 1000);

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 5: ‘)’ or ‘,’ expected
}

Total ActionScript Errors: 2 Reported Errors: 2

 
Comment by elchocolato
2007-02-18 08:11:51

@Chelion, you put the second “-” before the semi-colon. So:
_root.timer–;
Basically, — is a shortcut for subtracting 1.
You could also write _root.timer = _root.timer -1.
It’s just easier to use the shortcut.

 
Comment by elchocolato
2007-02-18 08:13:58

Oh, I see the confusion, two -’s in a row looks like one -. So to clarify:
_root.timer “-” and then another “-” and then “;”
without quotes

 
Comment by jock
2007-02-20 00:46:40

WHAT A LOUSY TUTORIAL!!

 
Comment by awesty
2007-02-20 18:42:14

@Chelion: Seriously, go back and READ the tutorial probably.

@Echocolata: Instead of

a = a - 1; you can just do

a -= 1; ;)

 
Comment by Kurt Subscribed to comments via email
2007-02-26 01:09:19

Ahh I’m way better then i used to be but still this doesn’t work for me! the error is:

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 4: Syntax error.
_root.timer–;-

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 5: Operator ‘-’ must be followed by an operand
}, 1000);

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 3: Statement block must be terminated by ‘}’
id = setInterval(function () {

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 5: Syntax error.
}, 1000);

Total ActionScript Errors: 4 Reported Errors: 4

and the code i put in is :

_root.timer = 100;
clearInterval(id);
id = setInterval(function () {
_root.timer–;-
}, 1000);

 
Comment by Kurt Subscribed to comments via email
2007-02-26 01:13:41

Oh i fixed something but the code is :

_root.timer = 100;
clearInterval(id);
id = setInterval(function () {
_root.timer–;
}, 1000);

And the error says :

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 4: Syntax error.
_root.timer–;

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 5: ‘)’ or ‘,’ expected
}, 1000);

Total ActionScript Errors: 2 Reported Errors: 2

 
Comment by Kurt Subscribed to comments via email
2007-02-26 01:16:01

Ahh no more errors but their is no timer the code i put in is :

_root.timer = 100;
clearInterval(id);
id = setInterval(function () {
_root.timer = _root.timer -1.
}, 1000);

and sorry about all the comments.

Comment by Mike Subscribed to comments via email
2007-06-06 07:15:19

KURT YOUR A GENIUS TY

Comment by Kurt Subscribed to comments via email
2007-08-13 22:41:29

How am I genius for that? I do way better stuff then that like make my website in flash and trying to make a game (i will figure out the problem sooner or later).

ya but that comment is 8 months old and i sucked alot more lol.

 
 
 
Comment by Kurt Subscribed to comments via email
2007-02-26 01:21:15

OK it shows numbers but the numbers won’t go down! The code is the same as above i just put timer in variable not instance name.

 
Comment by Kurt Subscribed to comments via email
2007-02-26 01:26:09

YA finally ok for those who can’t get it right put timer in var box and put this code on the page:

_root.timer = 100;
clearInterval(id);
id = setInterval(function () {
_root.timer -=1
}, 1000);

One thing it will go to -’s.

 
Comment by Kurt Subscribed to comments via email
2007-02-26 01:36:21

And who ever wants a timer to see how fast you can beat a level ( numbers going up instead of down )

The code is :

_root.timer = 0;
clearInterval(id);
id = setInterval(function () {
_root.timer +=1
}, 1000);

 
Comment by Kurt Subscribed to comments via email
2007-02-26 01:41:50

And this is for seconds and milla seconds going down one problem their are a bunch of 9’s at the end that i migth be able to fix
Well the code is:

_root.timer = 100.00;
clearInterval(id);
id = setInterval(function () {
_root.timer -=0.01
}, 1);

 
Comment by andy
2007-03-03 03:37:40

This may sound stupid but what do you mean dynamic text block

 
Comment by awesty
2007-03-05 16:50:27

@Kurt: Heh… Next time can you just use one comment? I just got like 10 emails because of you >_<;

@Andy: Make a text box and set it to dynamic.

 
Comment by Krytos
2007-03-18 11:42:57

Thanks for this tute Awesty, it’s been very useful and a great way of explaining how to use setInterval.

After a bit of play, I’ve gotten “if statements” and the sort to work. In short, the setInterval function creates it’s own “little world” within Flash, and only the code within setInterval will update at the interval you set (in this case, every second - hence the variable ‘timer’ is increased by 1 every second). In order to use this timer for something more than just numbers increasing, you’ll need to add extra code into the setInterval function. Growing on Awesty’s code, you could do this (NOTE: I’m counting up, not down):

_root.timer = 0;
clearInterval(id);
id = setInterval(function () {
_root.timer++;
//–HERE’S THE ADDED CODE–
if (timer > 10)
{
//Execute code
}
//–END ADDED CODE–
}, 1000);
This simply means that when timer is greater than 10 (10 seconds) do whatever code you’ve written within the if statement.

To keep things a little tidier, you could substitute in a function instead.

_root.timer = 0;
clearInterval(id);
id = setInterval(function () {
_root.timer++;
exampleFunction();
}, 1000);

function exampleFunction()
{
if (timer > 10)
{
//Do stuff here
}
}

I’ve been using this to adjust the game level on a game I’m coding - so as the time increases the game’s difficulty does as well. Here’s what I’ve been doing.

clearInterval(id);
id = setInterval(function ()
{
counter++;
changeLevel();
}, 1000);

//—functions—
function changeLevel()
{
if ((counter%10)==0)
{
//These are pre-set variables
//controlling the game
level++; //Change text
bikes++; //incease opponent number
motorbikeSpeed++; //increase speed
}
}

I hope this helps others :)

 
Comment by wilco
2007-03-29 03:36:52

onClipEvent (enterFrame) {
if (this.hitTest(square)) {
_root.circletimer = 0;
clearInterval(ID);
ID = setInterval(function () {
_root.circletimer += 1;
}, 1643.8356);
}

 
Comment by wilco
2007-03-29 03:38:54

whooops, that post above here is my code, for if a square and a circle hit eachother. I want to run a timer when they hit, but the timer doesn’t start running

 
Comment by akskater100
2007-03-30 07:32:34

“The actions on the clipboard contain errors. Actions with errors cannot be pasted into normal mode.”

What does this mean and how can i fix this?

 
Comment by Gankro
2007-03-31 09:01:59

this is nice but can be just as easly achieved by creating a variable called “timer” and then making a dynamic text with var: “timer” then creating a movie clip with the amount of frames that your frame is set… say 24… and on the 24 frame inserting a keyframe with this code:
_root.timer +=1;
and in the main timeline inserting:
_root.timer=0
or if you want it to count down change:
_root.timer +=1;
to:
_root.timer -=1;
and changing:
_root.timer=0
to:
_root.timer=100

 
Comment by awesty
2007-04-05 13:14:40

@wilco: Because while the square is hitting the circle you have put for the timer to equal 0.

@akskater100: Sorry but I have never gotten that error.

@Gankro: But this way is alot more organized, since all the code is in the one spot and you dont have to worry about MC’s and other things.

 
Comment by Andy
2007-04-09 21:21:52

Hi there

I’m having a problem with the timer showing on the actual screen when I publish it. The code I have is:

_root.timer = 30;
clearInterval(id);
id = setInterval(function () {
_root.timer–;
if (timer = 0)
{
gotoAndPlay(2)
}
}, 1000);

Ive placed this code on the same layer as the dynamic text box labelled timer, on the same keyframe.

Any help would be much appreciated.
Andy

 
Comment by awesty
2007-04-10 12:31:32

change if(timer = 0) to if(timer == 0).

 
Comment by Andy
2007-04-10 21:05:44

Legendary, All is working. Thankyou

 
Comment by mike Subscribed to comments via email
2007-05-01 14:17:47

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 5: Unexpected ‘}’ encountered
}, 1000);

Total ActionScript Errors: 1 Reported Errors: 1

what do i do??

 
Comment by mike Subscribed to comments via email
2007-05-01 14:23:24

What can I possibly be doing wrong???

root.timer = 100;
clearInterval(id);
id = setInterval(function () {
_root.timer–;
}, 1000);

Comment by awesty
2007-05-01 20:18:18

Where are you putting the code? did you add the extra hyphen and what version of flash do you have?

 
 
Comment by mike Subscribed to comments via email
2007-05-02 10:01:54

I am putting the code in the second frame where I wnat the timer to appear.

What second hypone?

I have Pro flash 8.

Comment by awesty
2007-05-04 14:07:05

If you read the comments it says you are suppose to add an extra hyphen to the line saying _root.timer–;

It just gets removed for some reason.

 
 
Comment by Kurt Subscribed to comments via email
2007-05-22 03:36:38

Awesty, I have made a button that code is,

on(release){
_root.timer = “”
_root.timer += random(30)+20;
}

i put “_root.timer = “”" because if it click on it if there is a number it will erase it and add a new random number ( i am using this for my http://kfb-zone.com/flash/Kurtssystem.swf project because it is stupid to get logs each click so i wanted every time you click it has a random number and when it is 0 you get a log then make it “”.)

so I made a new flash file to start making it,
so i tried this code but it doesn’t work,

_root.timer = “”;
if(_root.timer = “”){
_root.timer = “”;
//So the timer box does not say NAN but it does…
if(_root.timer = 0){
//this is where I’m going to add
//_root.lognormal/logoak += 1;
//_root.xp += 25/37.5;
//(those aren’t my real variables just for safety i made
//fake ones.)
_root.timer = “”;
//So I don’t get like 10x the xp and logs i am needing
//to get because it stays 0 for a second. also if i
//put this code above the code above i will never get
//logs or xp.
clearInterval(id);
id = setInterval(function () {
_root.timer -=1
}, 1000);
}
}
//i don’t know what I’m doing wrong please help me…
//Well it sorta works but one thing when i click the
//button the numbers don’t even go down!!!

Comment by Kurt Subscribed to comments via email
2007-05-22 03:39:49

Well if you use the code on your flash you might understand if you don’t, I can’t even understand that comment…

 
 
Comment by Kurt Subscribed to comments via email
2007-05-23 22:03:56

This is just a test to see if I can make 2 -’s
if this works i will say how it works

—-

 
Comment by Kurt Subscribed to comments via email
2007-05-23 22:08:07

Well at least when you copy and paste that it will show 2 -’s Awesty to do that click the - key 4 times.

 
Comment by Warrantica Subscribed to comments via email
2007-06-06 22:35:20

great tute! thanks a lot. i’ve learned a lot from it… ^^

 
Comment by Zephyr
2007-06-07 07:15:48

Nice tutorial, just a question, how do I make the time go up everysecond and when you die and go to frame 5 it tells you how long you’ve been alive.
Thanks in advance.

Comment by Kurt Subscribed to comments via email
2007-06-09 21:27:13

I have said this in comments before but if you want the time to go up instead of down put the actionscript,

_root.timer = 0;
clearInterval(id);
id = setInterval(function () {
_root.timer +=1
}, 1000);

and for the 5th frame actionscript it should be

clearInterval(id);
id = setInterval(function () {
_root.timer +=0
}, 1000);

with the same dynamic text box timer on the frame

and if you dont have a restart button add a button with the actionscript

on(release){
gotoAndStop(1);
}

Comment by Kurt Subscribed to comments via email
2007-06-09 21:28:07

non component button

 
Comment by Zephyr
2007-06-09 22:51:38

But should I call the dynamic textbox “timer”?It doesn’t work for me, the AS isn’t wrong (no errors) but nothing appears when I play, can you help me?Btw, I know how to make a replay button, don’t worry, I’m just crap at mathematical stuff like scoring.

Comment by Kurt Subscribed to comments via email
2007-06-23 00:56:40

Oh sorry i wasn’t on this website for a long time i was on mine lol well yes.

 
 
 
 
Comment by Warrantica Subscribed to comments via email
2007-06-08 22:17:44

yeah and 1 thing how i can make the score going up and when it reaches some limit, it goes level up.

i’ve tried but it can’t. no errors but when the time reaches the limit, it doesn’t do anything

Comment by Kurt Subscribed to comments via email
2007-06-09 21:53:52

well it depends how much score you want. if you want the score to go up 100 score per second the code on frame one should be

stop();
_root.timer = 0;
_root.score = 0;
clearInterval(id);
id = setInterval(function () {
_root.timer +=1
_root.score +=100
}, 1000);

if your score var is score if it is something else call it what it is. that is just for the score to go up with the time though you will need a level dynamic text box. then you should have the code on all frames up to the 4th ( if you want to go up to the next level with the same score every time.)

o srry i got stuck to now ask awesty

Comment by Kurt Subscribed to comments via email
2007-06-09 21:54:48

i just hope you didn’t have that stuff before so i didn’t do all that work for nothing

 
 
 
Comment by Bryce K Subscribed to comments via email
2007-06-10 03:16:53

Ahh, that is a great tutorial…

 
Comment by Bryce K Subscribed to comments via email
2007-06-10 04:44:29

opps, can any1 help me how to i stop the the setIntervals();? like if i want to pause the movie how would i make the intervals stop?

Comment by Sanchu Subscribed to comments via email
2007-06-10 05:46:31

yes i would like to know that too plz

 
 
Comment by Flickington Subscribed to comments via email
2007-06-10 10:44:04

Well, I was wondering, could you make the time only count (up, or down) AFTER a requirement is met? Such as the clicking of a button? I was wondering, because I only want my timer to count the seconds after the user starts the game/file, not before!
(Yes, there is a start button, this is why I’m asking)

Thanks

Comment by awesty
2007-06-15 14:24:37

_root.timer = 100;

function timer(){
clearInterval(id);
id = setInterval(function () {_root.timer–-;}, 1000);
}

onEnterFrame = function(){
if(requirement == true){
timer();
}
}

I have no idea if that will work or not. There should be two - after the _root.timer, so if there is only one add another.

Comment by Bryce K Subscribed to comments via email
2007-06-26 12:05:24

that doesn’t work…

Comment by awesty
2007-07-03 17:55:49

Hmm…

Try:

_root.timer = 100;

clearInterval(id);
id = setInterval(function () {REQUIRMENT==trye?_root.timer–-:0;}, 1000);
}

There should be two ‘-’ after timer.

 
 
 
 
Comment by Bryce K Subscribed to comments via email
2007-06-11 11:54:16

you can say
(actionscript 2 on a movieclip)

onClipEvent(enterFrame){
if(_root.REQUIREMENT == true){
gotoAndStop(WHAT EVER FRAME);
}
}

 
Comment by Kurt Subscribed to comments via email
2007-06-23 01:00:33

Awesty you didn’t answer my question ,Warrantica’s question and Bryce K’s question I think you don’t answer questions with a comment attached to a comment.

 
Comment by Mattitiah Curtis Subscribed to comments via email
2007-06-27 00:31:53

Could you guys please help me? I’m having certain
problems with the actionscript. Is is actionscript 2 frienly? Because I use flash 8.

Comment by awesty
2007-07-03 17:56:47

Yes, Actionscript 2 is friendly.

 
 
Comment by Mattitiah Curtis Subscribed to comments via email
2007-06-27 01:21:10

Never mind I fixed it. Try this everybody:

_root.timer = 5;
clearInterval(id);
id = setInterval(function () {
_root.timer–;
}, 1000);

Oh, yeah, make sure you remove awesty’s comments that are in the script.

 
Comment by Mattitiah Curtis Subscribed to comments via email
2007-06-27 02:04:31

Help! This is my actionscript but it does not go to the next frame nor stop at 0:

_root.timer = 5;
clearInterval(id);
id = setInterval(function () {
_root.timer–;
}, 1000);
if (_root.timer == 0) {
_root.gotoAndStop(2);
}

Comment by awesty
2007-07-03 17:57:48

Try this:

_root.timer = 5;
clearInterval(id);
id = setInterval(function () {
_root.timer–;
}, 1000);

onEnterFrame = function(){
if (_root.timer == 0) {
_root.gotoAndStop(2);
}
}

Remember to add a second ‘-’ after timer.

 
 
Comment by Kurt Subscribed to comments via email
2007-07-13 02:42:03

Awesty, I have made a button that code is,

on(release){
_root.timer = “”
_root.timer += random(30)+20;
}

i put “_root.timer = “”” because if it click on it if there is a number it will erase it and add a new random number ( i am using this for my http://kfb-zone.com/flash/Kurtssystem.swf project because it is stupid to get logs each click so i wanted every time you click it has a random number and when it is 0 you get a log then make it “”.)

so I made a new flash file to start making it,
so i tried this code but it doesn’t work,

_root.timer = “”;
if(_root.timer = “”){
_root.timer = “”;
//So the timer box does not say NAN but it does…
if(_root.timer = 0){
//this is where I’m going to add
//_root.lognormal/logoak += 1;
//_root.xp += 25/37.5;
//(those aren’t my real variables just for safety i made
//fake ones.)
_root.timer = “”;
//So I don’t get like 10x the xp and logs i am needing
//to get because it stays 0 for a second. also if i
//put this code above the code above i will never get
//logs or xp.
clearInterval(id);
id = setInterval(function () {
_root.timer -=1
}, 1000);
}
}
i don’t know what I’m doing wrong please help me…
Well it sorta works but one thing when i click the
button the numbers don’t even go down!!!

 
Comment by TheFlasher
2007-07-24 04:37:20

The timer works, until I get to the next frame. For some reason the timer is still going even when it’s not on the frame. Can you help me make the timer stop when it goes to the next frame? Thanks.

 
Comment by anders hafslund Subscribed to comments via email
2007-08-16 23:28:53

hey Awesty, thanks for a great tut.
how can i make the countdown increase if a movieclip hitTests another movieclip?

 
Comment by jamie Subscribed to comments via email
2007-10-11 03:05:43

hi, this is a great tut, but im having a of problem in that i test the movie and no matter what i put the timer or the minus value to it goes to 0 after 1 second, then goes back to frame 1

here is the code that i have put in
_root.timer = 1000;
//Makes the Timer equal 100. Change it to whatever number you want.
clearInterval(id);
//If this wasnt here, after the first second the function would run everyframe
id = setInterval(function () {
// Read below for the rest…
_root.timer = - 0.001;
// Add another hyphon onto the end
}, 1000);
if (timer

Comment by awesty
2007-10-16 16:59:54

Try changing it to

_root.timer -= 0.001;

 
 
Comment by Wilco
2008-02-10 04:39:40

When I copy and paste this onto the frame, I get these two errors:

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 4: Syntax error.
_root.timer–; //Add another hyphon onto the end

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 5: ‘)’ or ‘,’ expected
}, 1000);

Total ActionScript Errors: 2 Reported Errors: 2

I called the textbox “timer” and I’m using MX Professional 2004

Comment by Wilco
2008-02-10 04:57:04

Nevermind

 
 
Comment by jacob Subscribed to comments via email
2008-02-19 10:04:01

hey, after i make a dynamic text box it wont let me do any code…

 
Comment by rudydbooty
2008-07-19 06:31:41

wow sweet tutorial this saved me so much time!

 
Comment by hello ☺
2008-08-30 23:39:57

Im trying to make this…
if the timer (dynamic text)
goes to 0 go to another page
but this doesn’t work…
here’s the code:

onClipEvent (enterFrame) {
if(_root.timer<0) {
gotoAndStop(2);
}
}

Please help me!
Thnx

 
Comment by hello ☺
2008-08-31 19:43:30

I have fixed this problem ☺ !

Comment by Liam Subscribed to comments via email
2009-01-12 13:39:26

How’d you fix it?

 
 
Comment by sathya Subscribed to comments via email
2009-06-02 18:14:40

timer.text = 100;
function time() {
timer.text = timer.text-1;
}
setInterval(time, 10);

 
Comment by Julian Subscribed to comments via email
2009-09-08 20:42:32

stop();
_root.timer = 100;
clearInterval(id);
id = setInterval(function () {
_root.timer — ;
}, 1000);

this is the final code if u didnt add the hyphen urself

 
Comment by Julian Subscribed to comments via email
2009-09-22 20:02:35

by the way make sure your using minus not hyphens

 
Comment by Fabled Face Subscribed to comments via email
2010-02-16 08:36:25

Hey. First off I thought I’d tell you that I’ve been learning from your tutorials for the past couple of weeks, and so I should say thank you.

I have run into a few errors in scripts from your tutorials, (I can’t remember where) but they have all been easy to fix now that I know most of the meanings, but this time I found an error that I can’t figure out. I see that you were drowned in questions and concerns from people on this tutorial, but I couldn’t find anything to help me.

My problem is:
**Error** Scene=Scene 1, layer=Layer 1, frame=49:Line 5: Syntax error.
_root.timer —;

**Error** Scene=Scene 1, layer=Layer 1, frame=49:Line 6: ‘)’ or ‘,’ expected
}, 1000);

Total ActionScript Errors: 2 Reported Errors: 2

Maybe you can help? I don’t know how your site works with you, and the last comment on here is from last year, but if you do happen to still be around please help.

 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.