Home > Article > Web Front-end > How to stop the execution of a function in jquery
In jquery, you can use if statements and return statements in functions to stop the execution of the function. The return statement can terminate the execution of the function and return the value of the function. The syntax is "if (stop condition) {return;} ".
The operating environment of this tutorial: windows7 system, jquery1.10.0 version, Dell G3 computer.
How to stop the execution of a function in jquery
We can use the return statement to terminate the execution of the function and return the value of the function. Let's take a look at the operation of stopping the function through an example. The example is as follows:
Create a new html file and name it test.html to explain how jquery stops 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.
Open the test.html file in the browser, click the button to view the results.
Summary:
1. Use div tags to create a module for digital output display.
2. In js, 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.
Note:
The return statement will terminate the execution of the function and return the value of the function.
Recommended related video tutorials: jQuery video tutorial
The above is the detailed content of How to stop the execution of a function in jquery. For more information, please follow other related articles on the PHP Chinese website!