Wednesday, September 16, 2009

Flash ActionScript setInterval

Many a times we feel the need to set time intervel between frams so this is an example of it. Lets get started.

1) first we take a variable displayTime and that will be = to the duration

var displayTime = 5;

in here 5 is for 5 seconds hold, if you want then you can increase or decrease the time as you want.

2) Its time for us to create the function
so the name of the function is countDown

var displayTime = 5;
countDown=function(){

}

3) Next is to verify weather the function get loaded sucessfully and display the message after 5 sec.

var displayTime = 5;
countDown=function(message){

}

4) we create a loop function now so that the function starts countion reverse as for ex 5sec, 4sec, 3sec, 2sec, 1sec, and finally go

var displayTime = 5;
countDown=function(message){
displayTime--;
}

5) how would the function know weather it has finished counting or not , thus its time for condition.

var displayTime = 5;
countDown = function(message)
{
displayTime--;
if (displayTime == 0){

}
}

6) finallly you need to stop the loop function or else it would continue the loop function and you get confused, we use clearInterval to end the timer.

var displayTime = 5;
countDown = function(message)
{
displayTime--;
if (displayTime == 0){
clearInterval(timer);
trace("this action you need to paste here")
}
}
timer = setInterval(countDown, 1000);

the whole script would look like this

var displayTime = 5;
countDown = function(message)
{
displayTime--;
if (displayTime == 0){
clearInterval(timer);
trace("this action you need to paste here")
}
}
timer = setInterval(countDown, 1000);

No comments:

Post a Comment