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.
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.
Execution result:
##Extended knowledge: return statement
return 返回值;
Note:
//声明一个带返回值的函数 function getSum(num1, num2){ //函数体 return num1 + num2; } //可以通过变量来接收这个返回值 var sum1 = getSum(7, 12); // 函数返回值为:19 var sum2 = getSum(-5, 33); // 函数返回值为:28
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!