Making A Timer With setInterval();
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.
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















nice!
Thanks. ;D
but it goes to negative numbers…
is this how to solve it?
if(_root.timer
oopsy
it should go
if(_root.timer (LESSTHAN) 0){
_root.timer = 0;
}
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…
Yeah I got the same error, for the second one. **Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 6: ‘)’ or ‘,’ expected
}, 1000);
But there are only 5 lines of code…
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
You need to add an extra hyphen onto the end of the _root.timer-;
Also just put:
Math.Max(_root.timer,0);
Ya but it says that there is no Method with the name ‘Max’
pls help me
tnx
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
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?
cough cough…. awesty, your homepage timer is bellow -3000 now… might wana update
@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.
whats a hyphen?
the minus thingy?
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
@KIM: Yes.
@abhilbash:
awestyproductions[@]hotmail[.]com
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)
}
I dont think you can have the stop there, try removing that.
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.
kk, it doesnt worry me
AHH!!!!
i can’t belive im saying this but i give up on this freekin timer!
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?
@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.
hmm…………………………………………………………………………..
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.
you could do that with an if else statement, like:
If(timer is less then or equal to 0){
do something
}
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.
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.
Lol, when it reaches 0, it gets to -1, -2, -3 and so on xD
You can fix that with a simple if statement
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
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
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
@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.
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
WHAT A LOUSY TUTORIAL!!
@Chelion: Seriously, go back and READ the tutorial probably.
@Echocolata: Instead of
a = a - 1; you can just do
a -= 1;
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);
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
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.
KURT YOUR A GENIUS TY
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.
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.
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.
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);
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);
This may sound stupid but what do you mean dynamic text block
@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.
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
onClipEvent (enterFrame) {
if (this.hitTest(square)) {
_root.circletimer = 0;
clearInterval(ID);
ID = setInterval(function () {
_root.circletimer += 1;
}, 1643.8356);
}
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
“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?
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
@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.
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
change if(timer = 0) to if(timer == 0).
Legendary, All is working. Thankyou
**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??
What can I possibly be doing wrong???
root.timer = 100;
clearInterval(id);
id = setInterval(function () {
_root.timer–;
}, 1000);
Where are you putting the code? did you add the extra hyphen and what version of flash do you have?
I am putting the code in the second frame where I wnat the timer to appear.
What second hypone?
I have Pro flash 8.
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.
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!!!
Well if you use the code on your flash you might understand if you don’t, I can’t even understand that comment…
This is just a test to see if I can make 2 -’s
if this works i will say how it works
—-
Well at least when you copy and paste that it will show 2 -’s Awesty to do that click the - key 4 times.
great tute! thanks a lot. i’ve learned a lot from it… ^^
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.
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);
}
non component button
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.
Oh sorry i wasn’t on this website for a long time i was on mine lol well yes.
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
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
i just hope you didn’t have that stuff before so i didn’t do all that work for nothing
Ahh, that is a great tutorial…
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?
yes i would like to know that too plz
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
_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.
that doesn’t work…
Hmm…
Try:
_root.timer = 100;
clearInterval(id);
id = setInterval(function () {REQUIRMENT==trye?_root.timer–-:0;}, 1000);
}
There should be two ‘-’ after timer.
you can say
(actionscript 2 on a movieclip)
onClipEvent(enterFrame){
if(_root.REQUIREMENT == true){
gotoAndStop(WHAT EVER FRAME);
}
}
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.
Could you guys please help me? I’m having certain
problems with the actionscript. Is is actionscript 2 frienly? Because I use flash 8.
Yes, Actionscript 2 is friendly.
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.
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);
}
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.
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!!!
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.
hey Awesty, thanks for a great tut.
how can i make the countdown increase if a movieclip hitTests another movieclip?
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
Try changing it to
_root.timer -= 0.001;
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
Nevermind
hey, after i make a dynamic text box it wont let me do any code…
wow sweet tutorial this saved me so much time!
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
I have fixed this problem ☺ !
How’d you fix it?
timer.text = 100;
function time() {
timer.text = timer.text-1;
}
setInterval(time, 10);
stop();
_root.timer = 100;
clearInterval(id);
id = setInterval(function () {
_root.timer — ;
}, 1000);
this is the final code if u didnt add the hyphen urself
by the way make sure your using minus not hyphens
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.
Honestly!
Bunch of Newbs.
Learn Actionscript 3 already!
Heres an easy way…
var count = 100;
setInterval(decrease,1000);
function decrease(){
if(count > 0){
–count;//should be 2 - symbols here, but crappy commenting feature on this site removes one of the minus symbols. (check you have 2 of them before count)
trace(count);//or write to textbox etc
}
}