Learn the deep understanding function of Flash as

Source: Internet
Author: User
Tags anonymous array definition expression function definition variables valid variable scope
function to understand the purpose of a function
Passing arguments to a function
Understand the importance of the variable scope of a function
Writing a custom function
Returning a value from a function
To create a recursive function with SetInterval ()
By using functions, you can create reusable code, readable code, and smart code. With functions, you can write effective, finely structured, well maintained code, rather than lengthy, clumsy code.
I. Understanding using functions for programming
A function is an innovation. Writing code has no function, just like publishing books without printing presses, publishing industry is so unproductive, so there is no production. With a printing press, only one version, you can copy from that version of many copies. The printing press is an innovation. Similarly, if a program does not have a function, it has to write out each line of code one at a time. However, when you write a function, you can encapsulate multiple statements together, and you can repeatedly call that function (that is, the group of those statements) without having to write the same code again. That's a lot more ingenious. A function is a method of organizing a block of code that is not executed until it is invoked (directly or indirectly) from its main flow. In other words, a function is a method that wraps code blocks that execute (without invoking not executing) a particular task at call time. Functions are more advantageous than unstructured programming. These advantages include:
Make your code more readable by eliminating confusing and redundant code.
Make the program more efficient by reusing the function instead of typing the entire block of code at a time.
function becomes the center point for the modification. Make a change in the function, the modification can be applied to each instance that calls the function.
Writing mature functions can be reused in many programs. As a result, you can develop a library of functions that can be used to build a variety of programs without having to write scripts from scratch each time.
The code packaged in a function provides the basis for user interaction. If there are no functions, the application runs like a separate program. With a function, a user-initiated action can call a function. Ii. defining custom FunctionsWe already know some of the advantages of using functions in Acttionscript code. Now, you need to learn how to write a function. Writing a function is also called a "definition" or "declaration" of a function. Syntax for functions: function functions Name (parameters): Return data type {
function body
In the syntax of a function, there are several key points to note:
The function keyword tells Flash that an functions are being declared. When defining a function, you must include function keywords as above.
Functions should follow the naming rules for variables. As with named variables, it is a good idea to give a function a name that can represent its function.
The definition of all functions must include a pair of parentheses after the function name. The "arguments" in parentheses can be without. You can read more about the parameters later in this section. However, whether or not a function defines a parameter, you must include a pair of parentheses in the definition.
The parentheses are followed by a colon and a valid data type name. The data type is the type of data that the function will return. You'll see how to return the data later. Use void to indicate that the function does not return a value.
The function body is defined by a pair of start and end wavy braces ({}).
Now that we know the basic syntax, let's look at a very simple function example: function displaygreeting (): void{
Trace ("Hello.");
}

Third, call function
We define the term "function" as a block of code that is deferred execution. This means that a function can be defined, but nothing happens until it is accessed or invoked. You can test this yourself by creating a new Flash movie in frame 1th of the main timeline with the following code:

function displaygreeting (): void{
Trace ("Hello.");
When you test the movie, you see that although there is a trace () action in your code, nothing happens. So, now that you know how to "define" functions, you need to learn how to use them in your programs by calling them. In order to invoke a function, you need to use the name of the function, followed by a parenthesis (which is called a "function call operator"). When a function is called, the call to the function itself is a statement. Therefore, you should use a semicolon after the statement. The following example defines a function and then calls it. If you want to learn and test it yourself, simply place the code in frame 1th of the main timeline. function displaygreeting (): void{
Trace ("Hello.");
}
Displaygreeting ()//Call function when you test the movie, the following is displayed in the Output window:
Hello.

  Four, pass the parameter
Some functions do not need to pass any information to them. For example, the DLSPLAYGREETLNG () function in the previous section does not require any arguments. In other words, many functions need to pass arguments to them. For example, if the DLSPLAYGREETLNG () function can use different names to display personalized greetings, it makes the DLSP]AYGREETLNG () function more interesting. With the parameters, it's easy to do this. Modify the above function as follows:

function displaygreeting (name: String): void{
Trace ("Hello.") + name)
}

Once you have defined the function in this way, you can call it and pass a different parameter value to it. Some examples are shown below:
Displaygreeting ("John");//display: Hello. John
Displaygreeting ("Dick");//display: Hello. Dick
In a function, a parameter is a variable that assigns a value to the variable when it is called. As you can see in the displaygreeting () function, the parameter is named "Name", and the parameter is set to the value each time the function is called. When the function is called with the value "John", the variable is given "John", and the variable is given "dick" when the function is called with the value "dick". The arguments (variables) are declared in parentheses in the function definition. The reader may notice that the declaration of a parameter is similar but slightly different from the declaration of a regular variable. First, the similarity of the declarations is that you need to create a name for the variable and define a data type. However, when declaring a parameter, you do not need to use the var keyword, you do not need to use a semicolon, and you cannot initialize a parameter in parentheses. The following code is an example of an incorrectly declared parameter, and an error occurs:

The VAR keyword cannot be used.
function Displaygreeting (var name: String): void{
Trace ("Hello.") + name)
}
You cannot use semicolons.
function displaygreeting (name: String;): void{
Trace ("Hello.") + name)
}
Do not attempt to initialize variables in parentheses.
function displaygreeting (name: string= "Wang Maizi"): void{
Trace ("Hello.") + name)
What if you want to use more than one parameter in a function? Quite simply, when you define a function, you can declare multiple arguments separated by commas. Similarly, when calling the function, you can pass multiple values to it, simply separating the values with commas. The following is an example of a dlsp]aygreetlng () function with multiple parameters: function displaygreeting (name: String, Weight: number): void{
Trace (name + "weight is:" + weight);
}

Displaygreeting ("John", 50)//show: John's weight is: 50, the reader may notice that when you start adding more and more parameters to a function-defined parameter list, the code starts to go beyond the bounds of the editor. You can enable the automatic wrapping feature in the Actions panel. You can also place each parameter (or parameter group) in a new row when you define a function. This is a common practice because it makes it easier to read a list of arguments for a function composed of many parameters. The syntax is the same, except that each parameter in the argument list is placed in a new row so that it is easier to read. For example:

function displaygreeting (name: String,
Weight: number): void{
Trace (name + "weight is:" + weight);
}

Note: The number of parameters that define a function is not necessarily the same as the number of arguments passed when it is invoked, and of course, if the parameter is not available, the function may not work correctly, and multiple parameters are "in order" in sequence.
1. Pass values and references to parameters.
When passing arguments to a function, arguments are passed in one of two ways: by value and by reference. The difference is related to the data type.
(1) Basic data types, such as String,number,boolean, are passed by value. That is, the value is passed to the function, and any connection to the variable from which the value originates is cut off. In other words, when the value is passed to the function, any variables that are used to pass the values are independent. Here is an example:

Function Reference Test (a:number): number{
a++;
return A;
}
var b:number=5;
var c:number= reference Test (b);
Trace (b);
Trace (c);

The Output window will display the following contents
5
6
In this example, although the value of B is passed to the function, and that value is increased by 1 in the function, B retains its value (5). Why? Because the value of B is passed to the function, not to the variable itself. The value is then given a parameter named a in the function, which is incremented and returned. The returned value is then given a new variable named C.
(2) When reference data types are passed as parameters, they are passed by reference. This means that an object that is passed to a function is a reference to the actual object. As a result, anything you do with an object reference in a function affects the object itself. No copy of the object was generated. The following is an example of a MovieClip instance using the name mbox:

function Move (ma:movieclip,x:number,y:number): void{
Ma._x=x;
Ma._y=y;
}
Move (MBOX,100,100);

This example moves the MovieClip object named Mbox to the stage (100,100).

2, the use of arguments properties.
The function you see now either does not use any arguments, or the argument is declared as a list of parameters in parentheses. However, regardless of whether a function declares any arguments, all parameters passed to the function are saved in a special array named arguments. Each function has a arguments variable (object) that is created in the function when the function is called. ActionScript does not force the number of parameters in a function definition to match the number of arguments passed to the function at call time. This means that any value that is not passed at the time of the call but is defined in the parameter string of the function will have a undeflned value, and any value passed in the function call is ignored if it is not in the function's parameter definition. Therefore, you can define a function that has no parameters, but you can still pass parameters using the arguments object. Here is an example of using a arguments object as an array: function Traceparams (): void{
For (i=0;i<arguments.length;i++) (
Trace (Arguments[i]);
}
}
Traceparams ("One", "two", "three"); In this example, the following is displayed in the Output window:
One
Two
Three
In most functions, declaring parameters is much better. The arguments object is useful when overloading a function or encountering a similar situation (see the "Overloaded Functions" section). Each arguments object has two properties of a special reference function. Although these properties are not commonly used (caller and callee), these properties are useful in some cases, especially when developing highly abstract functions. If a function calls the current function, the caller property of the arguments object returns a reference to the function. The caller property has a null value if no other function calls the current function.
function function1 (): void{
Function2 ();
}
function function2 (): void{
if (Arguments.caller==function1)
Trace ("function2 called from Function1");
Else
Trace ("function2 not called from Function1");
}
Function1 ();//output:function2 called from function1
Function2 (); The arguments property of the callee object of//output:function2 not called from function1 a function is a reference to the function itself. It may not be immediately apparent why this is said to be useful. However, consider the case of anonymous recursive functions. You can write a function that you can recursively call yourself as follows:
Var ffactorial:function =function (noperand:number): number{
if (noperand>0) {
Return Noperand * Arguments.callee (NOPERAND-1);
}else{
return 1;
}
} return a value from a function
So far, the main introduction of the function as a subroutine this point. That is, a function can decompose the main program into smaller, more manageable fragments. On the one hand, when a function is in that way as a subroutine, the function does not need to return a value. On the other hand, sometimes you want to create a function that uses it for some calculations or operations, and then returns a value. You can use a return statement in a function that returns a specific value. The syntax for the return statement is as follows:
return value;
When returning a value from a function using the return statement, you should specify the data type to be returned (specified after the parentheses in the function definition). In the previous example, the return type is void (means no return). However, when a string is returned, the returned data type should be set to string, and when a number is returned, the returned data type should be set to numbers, and so on. Here is an example of a function that computes a rectangular area, and the return value is a number: function size (na:number,nb:number): number{
var Narea:number=na * NB;
return Narea;
Flash exits the function whenever a return statement is encountered. Therefore, if there are other remaining code behind the return statement, they will not be executed again. For example: Function area (na:number,nb:number): number{
var Narea:number=na * NB;
return Narea;
Trace ("The area is:" +narea);
Find the area (6,6); In the example above, the trace () statement is always not executed. This is because the code behind the return statement in the function is not executed. Here is an example of using a few return statements. Obviously, only one of the return statements can be encountered in any call to the function. In this example, a return statement is encountered when the condition is satisfied and the other return statements are encountered when the condition is not satisfied. The function accepts two parameters: an array of (strings), a string value. The function uses a For statement to search through the array until it encounters a member that matches the string. Once a match is found, it returns the corresponding index. If no match is found, the function returns NULL. function Findmatchingindex (atitles:array,stitle:string): number{
Loops through all the members of the array.
for (Var i:number=0;i<atitles.length;i++) {
Returns the corresponding index if one of the members matches the value of the Stitle. This causes the function to stop executing.
if (atitles[i]==stitle) {
return i;
}
}
If no match is found, the statement is encountered (that is it).
return null;
No matter what the function does, as long as it returns a value, it can be called entirely as part of an expression. For example, you can use the "Find area" function in the following ways: Var narea:number= for area (6,6);//return value to variable
You can also:
var narea:number= for area (6,3) * 5;//function to participate in computation essentially, the function becomes a value similar to a string, a number, a variable. Therefore, although the following is valid, it is a less useful ActionScript statement:
6;
The following are the same:
To find the area (6,6);
We want to use the returned value in some meaningful way. You can use a function that returns a value as if you were using a variable. We've seen the "Find area" function in assignment statements. Here is another example, in which the function is used as part of a conditional expression: if (6,6) >18 {
Trace ("The" is more than 18);
Note: The return of the function "value" can not only be numeric, string, etc., can also be objects, such as group, object and so on.

  Six, reference functions
You can use the name of a function to refer to a function. When the function name is used in conjunction with the function invocation operator (parentheses), the function is invoked, but the name itself is only a reference to the function. This means that you can actually use the name of the function to assign a reference to a variable. For example, once a reference to a function is given to a variable, you can call that function by combining that variable name with the function invocation operator. Here is an example:


function Find Area (na:number,nb:number): number{
var Narea:number=na * NB;
return Narea;
}
var farea:function= to seek area;
Trece (Farea (6,6));//36

Here you'll see how to assign an anonymous function to a variable.
Vii. Creating anonymous functions
Now you know how to use standard, named function syntax to define functions. In addition, there is a way to define functions using anonymous functions, which allows you to create a function without a name. You can then assign the function to a variable. The following is the syntax for anonymous functions:

function (parameter): return type {
function body
};

The reader may have noticed that standard function declarations and anonymous function declarations are syntactically similar, with only two different points. First, the anonymous function has no function name. Second, anonymous functions should be followed by a semicolon, which is not required in the declaration of standard functions. As mentioned earlier, this is primarily about assigning an anonymous function to a variable. Otherwise, when the function is defined, it "leaves" the scope (that is, undefined). Here is an example of assigning an anonymous function to a variable:

var fsayhi:function=function (sname:string): void{
Trace ("Hi," +sname);
);
Fsayhi ("Joey");//display: Hi,joey

As you can see, you could invoke the anonymous function by using the variable assigned to the anonymous function. In fact, anonymous functions are often used, recall, the function of the button event, such as:

Btn.onpress=funceton () {
......
}
Viii. Understanding the scope
Scopes are the scopes of certain identifiers defined in ActionScript. Some identifiers are defined only in one timeline, and some are defined in the scope of the entire movie, and some identifiers are defined only in one function. In a function, there are two scopes that need to be explained: The scope of the variable, the scope of the function. "Scope of a variable" is the scope of a variable in a function, and "Scope of a function" is the scope of a function in a movie.
1, the scope of the variable
When a variable is declared appropriately in a function, the variable is called a "local variable." A local variable means that when a variable is declared in a function, its definition is not persisted after the function call. This is a good way to avoid naming conflicts with other variables. The following is an example of a function that declares and initializes a local variable named smessage. The local variable is defined in the function. However, if you want to use trace () to display the value of the variable outside of the function, the result is undefined.
function Testscope (): void{
var smessage:string= "hello,world!";
}
Testscope ();
Trace (smessage);//display: Undefined in a large program with many functions, using local variables helps ensure that conflicts between variables with the same name are reduced. Although you should always try to take a unique name for a variable, it is possible to reuse the same variable name in different functions. If each has the same scope, one interferes with the other, resulting in an expected value and result. Another possible reason for using local variables is for memory management. Although it may not be really large, each variable defined in the program consumes memory. If you don't do something with a variable, but it's still defined, it wastes memory. By using local variables, the memory is freed when the function is finished. A parameter is considered a local variable, that is, its scope is in the function, but it is not outside it. This can be seen from the following example:
function Testparameterscope (smessage:string): void{
Trace (smessage);
}
Testparameterscope ("Hello"); Show: Hello
Trace (smessage);//display: Undefined unlike this, a variable declared outside the function (but in the same timeline that defines the function) can be used in that function, for example:
function Testscopetimeline (): void{
Trace (smessage);
}
Var smessage:string= "Hello";
Testscopetimeline ()//display: Hello in this example, the variable smessage is declared outside of the function, but can still be used within the function.
ix. Scope of functions
As is now known, when declaring a function, its scope is limited to the timeline in which it is declared. That is, you can call it through its name in the same timeline, and if you use a target path, you can call it outside that timeline. It is very easy to use this function in the same timeline. Using this function in another timeline is a bit inconvenient. Using this function in an object that does not have a timeline becomes challenging. Give it a try!
10. Create recursion
Recursion refers to a function that calls itself in its function body. In some cases, this is a necessary method. The classic example of recursion is to compute the factorial of a number. As a review, the factorial of number n refers to the following formula:
N (n-1) * (n-2) ... *1For example, the factorial of 5 is 120 (that is, 5*4*3*2*1). Here's a function to do this:
function factorial (n:number): number{
if (N>o) {
Return n*factorial (n-1);
}
else{
return 1;
}
Recursion is a fairly simple concept, but it's also a headache for people who haven't written a lot of code yet. So sometimes it may be a little blurry. To figure out how recursion works in this example, let's look at what happens when the function is called. This time, we use a small number to make it simpler: trace (factorial (3)), which is invoked when the first call to the factorial () function is made with the value 3. Because n is greater than 0, it executes the statement in the IF statement. This statement indicates that the function returns the value of an expression n*factorial (n-1). In order to evaluate the expression, the function must call itself (factorial (n-1)). This time, when factorial () is invoked, it is invoked with the value of 2. Again, the value of n is 0, so the first Rettirn statement is executed. The function calls itself again. This time, it is called with the value of 1. Repeat the same processing, and then call Factorial () again with the value O. However, in this function call, because n is no longer greater than 0, it returns 1 and no longer calls the function. You should be very careful to ensure that recursive functions have a limit on the number of recursion. Imagine what would happen if the function of this example were written as follows: function factorial (n:number): number{
Return n*factorial (n-1);
The function will call itself forever. This infinite loop can often cause the system to crash. Fortunately, Flash has a protective measure that disables ActionScript in the movie after a set number of recursive times. If you use this infinite recursion in the movie (that is, there is no condition that will stop the recursion), you will see the warning in the Output window when you test the movie.
11. Overloaded function
Overloaded functions are arguments that make multiple functions have the same name but have different numbers and/or types. In many cases this may be useful. For example, there might be a function named Caiculatearea () that calculates the area of a rectangle based on two parameters (the length of each edge). But you might also want to have a calculatearea () function that calculates the area of a circle based on a single parameter (RADIUS). The problem is, as already mentioned, ActionScript does not require the number of parameters in the function definition to match the number of arguments passed to it. This means that there cannot be two functions with the same name, even if they have different numbers of arguments. Therefore, you cannot really overload a function with ActionScript. Instead, you can use an if statement or a switch statement in a function to check the number of parameters to mimic the overload of a function. Here is an example of how you can write a function that calculates the area of a rectangle or the area of a circle based on the number of arguments passed to it (determined by arguments.length). This is not a strictly overloaded function, but the equivalent of ActionScript.
function CalculateArea (): number{
Switch (arguments.length) {
Case 1:
var nradius:number=arguments[o];
Return (math.pi* (Nradius*nradius));
Case 2:
var na:number=arguments[o];
var nb:number=arguments[1];
return (NA*NB);
Default
return null;
}
}
12. Write functions for reusable purposes
When writing a function, remember the importance of EASY-TO-USE or reusable code. Ideally, the function should be as versatile and packaged as possible. Functions should normally operate like a black box. This means that the activity of a function should essentially be independent of other parts of the program. A well written function should be able to be used in many different programs, just like a universal key can open a different lock. Functions should be written using the concept of reusability. When writing a common function, remember the following points:
In general, do not use variables defined outside functions--variables (and objects) used in functions should be declared in a function or passed as arguments to a function. If you need to assign a value to a variable that will be used outside the scope of the function, consider using a return statement instead. Because a function can only return one value at a time, it may be found that using a returns statement seems a bit limited. If that's true in a function, then maybe one of the following two things will happen: either the values you want to return are related, you can put them in an array or an object, and then return them, or they are unrelated values, so you should split the function into multiple functions. But there may be exceptions. At some point, you just want to organize some of the features in a movie together in a single function to organize your code into subroutines. In this case, it is acceptable to directly access variables and objects declared outside of the function.
Give a function a name that describes its task--when you see the function again, it's easy to know what the function is. If it's not easy to name a function because it does a lot of things, consider splitting that function into multiple functions. Even common functions, they should perform specific tasks. While these guidelines are often useful, it is sometimes not appropriate to write a truly generic function. If the task of writing a function is very special to the program being written, it may not be useful to write it too generic.
13. Using Built-in functions
You've learned how to create a custom function in ActionScript. Usually, when people talk about functions, they refer to these custom functions. However, there are a number of other "built-in functions" in ActionScript that can be used essentially like a custom function. The Actions Toolbox contains a folder named Global functions, which is a subfolder that contains all the built-in functions. Many of these functions have been replaced by classes and methods, so it is best to use these new alternatives. For example, all timeline control, movie clip control, and print functions have been replaced by methods. However, there are still some global built-in functions that are useful. These functions include:
Fscommand () _ one uses this function only in very special circumstances. The Fscommand () function enables Flash movies to communicate with the player.
SetInterval ()/clearinterval ()--these functions can instruct flash to call other functions at a specific, fixed interval. See the details in "Creating an interval function" below.
Escape ()/unescape ()-These functions are used to convert between text and secure URL formats.
The Gettimer ()--gettimer () function returns the number of milliseconds since the Flash movie began playing. This can be useful in some timed processing (not requiring high accuracy and accuracy). For example, to do some loops after the movie "Time Is Up", in which case, Gettimer () is an appropriate function. For example, there might be a movie waiting for a response from the server, but if you don't get a response after 30 seconds, you might want to stop waiting and remind the user that the server is not responding.
The Trace ()--trace () function is seen everywhere in this book. It is useful when testing flash applications to display information.
Isfinite ()/isnan ()--These functions test whether a value is finite, or even a valid number.
Parsefloat ()/parselnt ()--These functions parse a number from a string. 14. Create Interval function
One of the most useful things you can do with a function is to create an interval function using the setinterval () command. By using the SetInterval () function, you can specify a function and the interval (in milliseconds) that a continuous call to the function takes. The function returns an ID (identity) that can be used to stop the interval at a later time. The following is the standard syntax for using the SetInterval () function with a function: SetInterval (Function,interval[,param1...,paramn]) setinterval () The 1th parameter of the function should be a reference to a function. This means that you should not include the function call operator, that is, you cannot have parentheses. The interval parameter is measured in milliseconds. If you pass 1000 to the interval parameter, the function is called 1 times per second. You know, however, that the interval at which a function is invoked is imprecise. Flash calls functions as often as they are spaced. However, there is a conflict between how accurate the processor of the computer running the player and how precise the interval should be. By using the setinterval () action, you can pass parameters to the function randomly. Any arguments that are passed to SetInterval () immediately after the first two arguments (required) are then passed to the function. For example, the following example shows a writemessage () function that uses two parameters. By using SetInterval (), you can tell Flash that the function is called every 1000 milliseconds and two values are passed to the function: function Writemessage (sname:string,smessage:string): void{
Trace ("Hello," "+sname+".) +smessage);
}
var nwriteinterval:number=setinterval (writemessage,1000, "Joey", "Good morning."), when the Flash developer uses setinterval (), A common mistake is to assume that the variable passed to the function is evaluated by setinterval () each time the function is called. For example, the above code may be rewritten: function Writemessage (sname:string,smessage:string): void{
Trace ("Hello," "+sname+".) +smessage);
}
var snameparam:string= "Joey";
var smessageparam:string= "Good morning."
var nwriteinterval:number=setinterval (Writemessage,1000,snameparam, Smessageparam); The user may think: Changing the value of Snameparam or Smessageparam will result in different values being displayed in the Output window. However, the value of the variable Snameparam and Smessageparam is not computed each time the Wrltemessage () function is invoked. Instead, they are counted once when setlrlterval () is invoked. Those values are then used for each call to the function. Therefore, although the values of these variables are changed, the same values are still passed to the function. Next, let's look at an example that you can use to check the accuracy of the flash call interval function. You can place the code in frame 1th of the main timeline of a new flash document. function Tracetimer (): void{
Trace (Gettimer ()),
}
var ntimerinterval:number=setinterval (tracetimer,200); When you test it, you'll see that the function is called regularly, at approximately 200 millisecond intervals. But the interval is imprecise. If you use SetInterval () to influence animations in the stage, the frame rate may also have an effect on how to use SetInterval (). The rate at which the movie refreshes the stage is only visually equal to the frame rate of the movie. This means that if some processing occurs in the movie at a rate higher than the frame rate, it will not affect the display in the stage. Therefore, if you use SetInterval () to move a MOVIECLLP instance over the stage, and the frame rate is set to 1fps (the number of frames per second), but the function is called at intervals of 10 milliseconds, then the movement on the stage may wave. As shown in the following code, you can patch this by using the updateafterevent () action in the function that is being called by SetInterval (). The updateafterevernt () action instructs Flash to refresh the display regardless of the frame rate. function MoveRight (ma:movieclip): void{
ma._x++;
Updateafterevent ();
}
Var Nmoverinterval=setinterval (moveright,10, mcircle); You can also define an anonymous function in SetInterval (), which does not require passing the name of a function or a reference to a function.
var ntimerinterval:number=setinterval (function () {Trace (Gettimer ());},200);
Now that you know how to set an interval for a function to be called, the reader may also want to know how to stop a function that is being called constantly. In other words, just want to know how to clear the interval. You can do this very simply by calling the Clearinterval () function, and the clearinterval () function uses a separate parameter that represents the ID of the interval that should be cleared. Keep in mind that setinterval () returns an ID that can be used to indicate the interval. The following code stops an interval, and the ID of the interval has been given Ntlmerinterval: Clearinterval (Ntlmerinterval);
15. Summary
A function is a way to organize blocks of code together, and you can call them by name or reference to use them over and over again.
Functions can be subroutines, that is, they can perform certain operations and return a value.
Functions can be named or anonymous. Both of these types have different advantages and disadvantages.
By using the arguments object created for the function, you can call a call function, call an anonymous function recursively, and use the arguments passed to the function by array instead of by individual variables.
It is worthwhile to define common functions that can be used in many different environments.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.