Home >Web Front-end >Front-end Q&A >What to use to end function in jquery

What to use to end function in jquery

青灯夜游
青灯夜游Original
2022-11-02 19:37:511141browse

jquery uses if statements and return statements to end functions; you only need to use the if statement to set the end condition in the function. If the condition is met, use the return statement to terminate the execution of the function and return the value of the function. Syntax "if (stop condition) {return return value expression;}", the return value expression can be empty, that is, the function ends and no value is returned.

What to use to end function in jquery

The operating environment of this tutorial: windows7 system, jquery3.6.1 version, Dell G3 computer.

In jquery, you can use if statements and return statements in functions to stop the execution of the function.

Implementation idea:

  • Use if statement to set the end condition in the function

    • If it is satisfied This condition uses the return statement to terminate the execution of the function

    • If the condition is not met, execution continues

Implementation example

Let’s take a look at the operation of the stop function through an example. The example is as follows:

Create a new html file named test.html to explain jquery How to stop function execution. Use div tags to create a module for digital output display. Create an id attribute for the div tag, which is used to obtain the div object below.

In the test.html file, create a button button, bind the onclick click event to the button, and when the button is clicked, execute the gofunc() function.

In the gofunc() function, use a for loop, output the number through the append() method, use the if statement to determine if the variable i is greater than 5, and use return to stop the execution of the function.

What to use to end function in jquery

Execution result:

What to use to end function in jquery

##Extended knowledge: return statement

The return statement has two functions in the function definition:

  • One is to return the function value;

  • The second is to abort the execution of the function.

The return statement is usually defined at the end of the function. Grammar format:

return 返回值;

  • Among them, "return value" is an optional parameter, which can be a specific value or expression, or it can be empty. The "return value" and the return keyword need to be separated by a space.

The return statement can return any type of value including basic data types, objects, functions, etc. Every function returns a value. When the return statement is not used, or when return is used but no return value is specified after it, the function will return an "undefined" value. If you need to return a value other than "undefined", you must use return and specify the returned value.

Once the function executes the return statement, it will immediately return the function value and terminate the execution of the function. At this time, the code after the return statement will not be executed. According to this characteristic of the return statement, when the execution of the function needs to be exited early, a return statement without a return value is often used to terminate the execution of the function at any time.

Note:

  • #For functions that have a return value, we can use a variable to receive the return value of this function. The sample code is as follows:

  • //声明一个带返回值的函数
    function getSum(num1, num2){
            //函数体
        return num1 + num2;
    }
    //可以通过变量来接收这个返回值
    var sum1 = getSum(7, 12);      // 函数返回值为:19
    var sum2 = getSum(-5, 33);     // 函数返回值为:28
  • If the function does not contain a return statement, after each statement in the function body is executed, an undefined value is returned.

What to use to end function in jquery

[Recommended learning:

jQuery video tutorial, web front-end video]

The above is the detailed content of What to use to end function in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn