Flash – AS3 SetInterval()

flash-logo

SetInterval() Method in AS3

setInterval() is an ActionScript method which lets you execute a specific code repeatedly through a certain time interval in milliseconds. In order for setInterval() to execute code, that code must be run through a Function.

Sample code to call setInterval() in AS3

setInterval (myFunction, myTimer);

we can output the word “Hello World” once every two seconds by creating a function that outputs the word “Hello World” and then pass this function to our setInterval() method.

function helloFun():void {
trace(“Hello!”);
}
setInterval(helloFun,3000);

The function is first executed after 3000 milliseconds elapse (i.e. 3 seconds) and then again after each 3000 milliseconds forever. As an output you will see “Hello!” traces in your flash output console.

You can learn more about trace() in AS3 using our tutorial.


Use clearInterval() in AS3 :

clearInterval() is a method that can be used to stop a setInterval() method. You can use that on a setInterval() method that is stored in a variable.

This can be done using the regular var keyword.

Sample Code in AS3 using setInterval() and clearInterval() :

function helloFun():void {
trace(“Hello!”);
}

var myInterval:uint = setInterval (helloFun, 3000);

It is now possible to stop this interval at any time by passing the variable name of our setInterval() method to the clearInterval() method this way:

function helloFun():void {
trace(“Hello!”);
}

var myInterval:uint = setInterval (helloFun, 3000);
clearInterval(myInterval);

Please send us feedback below if you have liked our article. Hope this is very helpful to you!