Summary of setInterval usage in JavaScript

  • 2020-03-29 23:54:27
  • OfStack

The setInterval action calls a function, method, or object at regular intervals while the animation is playing. You can use this action to update the variables from the database or the update time display.

The syntax of the setInterval action is as follows:
SetInterval (function, the interval [, arg1, arg2, argn]...)
SetInterval (object, methodName, interval [, arg1, arg2, argn]...)

The first is the default syntax for the setInterval function in the standard actions panel, and the second is the method used in the expert mode actions.

The argument function is a function name or a reference to an anonymous function. The object parameter specifies the object derived from the object object. MethodName specifies the method to be called in the object parameter.

Interval specifies the time in milliseconds between calls to function or methodName. The following arg1, etc., is an optional argument to specify the arguments passed to the function or methodName.

SetInterval sets the interval to be less than the animation frame rate (say, 10 frames per second, or 100 milliseconds), and then calls the function as close to the interval as possible.

You must also use the updateAfterEvent action to ensure that the screen is refreshed with sufficient frequency. If the interval is greater than the animation frame rate, it is called only when the playback head enters a particular frame to minimize the impact of each screen refresh.

The following example calls an anonymous function every 1 second.
SetInterval (function(){trace(" I show this every 1 second ")},1000; // function(){} here is a function without a function name. The next 1000 is the interval in milliseconds.

The following example shows us how to run with parameters.


function show1(){
    trace(" every 1 Display once per second ");
}
function show2(str){
    trace(str);
}
setInterval(show1,1000);
setInterval(show2,2000," every 2 I'll show it once in a second ");

The setInterval method of the function has been described above.

Next we'll look at the setInterval method for the object.

First, write an example of setInterval calling an object's methods in action without passing arguments.


myobj=new Object();//Create a new object
myobj.interval=function){
    trace(" every 1 Display once per second ");
}//Method to create an object.
setInterval(myobj,"interval",1000);//Sets the time interval to invoke the method of the object.

Here's how to pass the parameters. It's the same thing as the passing parameter of a function.

myobj=new Object();
myobj.interval-function(str){
    trace(str);
}
setInterval(myobj,"interval",2000,"  every 2 I'll show it once in a second ");

Pay attention to. To invoke methods defined for an object, you must use the second syntax format in expert mode.

In that case, let's make a dynamic display of time. This can be done with the following code.


setInterval(show,1000);
function show(){
    time=new Date();
    hour=time.getHours();
       minu=time.getMinutes();
       sec=time.get.Seconds();
    datetime=hour+":"+minu+":"+sec;
}//Datetime here is the name of a variable for a dynamic text box.

So setInterval is a method that you should be pretty good at. Now, let's learn clearInterval.

The clearInterval action is used to clear the call to the setInterval function, which is syntactically formatted as clearInterval(intervalid); Intervalid is the object returned after calling the setInterval function.

Here's a simple example.


function show(){
    trace(" Display every second ");
}
var sh;
sh=setInterval(show,1000);
clearInterval(sh);


Related articles: