The setInterval action is used to call a function, method, or object every certain time when an animation is played. You can use this action to update the variables or update time display from the database.
The syntax format of the setInterval action is as follows:
SetInterval (function, interval [, arg1, arg2,... argn])
SetInterval (object, methodName, interval [, arg1, arg2,... argn])
The first format is the default syntax of the setInterval function in the standard action panel, and the second format is the method used in EXPERT mode actions.
The Parameter function is a function name or a reference to an anonymous function. The object parameter specifies the Object derived from the object. MethodName specifies the method to be called in the object parameter.
Interval specifies the time between two calls to a function or methodName, in milliseconds. The following arg1 and so on are optional parameters used to specify the parameters passed to the function or methodName.
SetInterval: the time interval set by setInterval is smaller than the animation frame rate (for example, 10 frames per second, equivalent to 100 milliseconds), and the function is called at the interval as close as possible to interval.
The updateAfterEvent action must be used to ensure that the screen is refreshed at sufficient frequency. If interval is greater than the animation frame speed, it is called only when the playback header enters a certain frame to reduce the impact of screen refreshing.
The following example calls an anonymous function every one second.
SetInterval (function () {trace ("I will display it once every 1 second")}, 1000); // The function () {} Here is a function without a function name. To become an anonymous function, the next 1000 is the time interval, in milliseconds.
The following example shows how to run with parameters.
Copy codeThe Code is as follows:
Function show1 (){
Trace ("display once every 1 second ");
}
Function show2 (str ){
Trace (str );
}
SetInterval (show1, 1000 );
SetInterval (show2, 2000, "I will display it once every 2 seconds ");
The setInterval method of the function has been introduced above.
Next we will introduce the setInterval method of the object.
First, write an example of setInterval to call the object method in an action. This example does not need to pass parameters.
Copy codeThe Code is as follows:
Myobj = new Object (); // create a new Object
Myobj. interval = function ){
Trace ("display once every 1 second ");
} // Method for creating an object.
SetInterval (myobj, "interval", 1000); // sets the interval to call the object method.
Next we will introduce how to pass parameters. In fact, the principle is the same as the parameter passed by the function.
Copy codeThe Code is as follows:
Myobj = new Object ();
Myobj. interval-function (str ){
Trace (str );
}
SetInterval (myobj, "interval", 2000, "I will display it once every 2 seconds ");
Note. To call a method defined as an object, you must use the second syntax format in EXPERT mode.
In this case, we will make a dynamic display of time. You can use the following code.
Copy codeThe Code is as follows:
SetInterval (show, 1000 );
Function show (){
Time = new Date ();
Hour = time. getHours ();
Minu = time. getMinutes ();
Sec = time. get. Seconds ();
Datetime = hour + ":" + minu + ":" + sec;
} // The datetime here is the variable name of a dynamic text box.
In this case, the setInterval method should be well learned. Now, let's learn clearInterval.
ClearInterval is used to call the setInterval function. The syntax format is clearInterval (intervalid). intervalid is the object returned after the setInterval function is called.
The following is a simple example.
Copy codeThe Code is as follows:
Function show (){
Trace ("display once every second ");
}
Var sh;
Sh = setInterval (show, 1000 );
ClearInterval (sh );