Jquery $. Ajax () and other success: function () {} causes the return problem.

Source: Internet
Author: User

First look at the Code:

// Ajax follow/cancel function focus_action (UID, type) {var R; type = type? 'New': 'delete'; $. ajax ({URL: 'api/follow/'+ type, data: {'uid': uid}, datatype: 'json', type: 'post', async: false, success: function (e) {r = common_return (E );
Return R; // return is used here to end the function success: function. Instead of the end of the entire function focus_action. }); Return r; // here return is the end of the focus_action () function .} // Public return true/falsefunction common_return (e) {If (false = e) {return false;} else {return true ;}} alert (focus_action (1, 1 ));

 

Conclusion: In JS, pay attention to the use of return, which can terminate the running of the program. When using it, pay attention to its function scope: specifically, it plays a role in that function.

 

 

// The following is the conversion from: http://blog.sina.com.cn/s/blog_67aaf4440100p3zg.html

 

In the object-oriented programming syntax, we will encounter the break, continue, and return keywords. What are the specific operations for using these three keywords? When using these three keywords, what are the rules that need to be understood? Let's start with the introduction:

 

Break Statement of JS programming Syntax:

The break statement causes the running program to immediately exit the loop contained in the innermost layer or exit a switch statement.

Because it is used to exit a loop or switch statement, this form of break statement is valid only when it appears in these statements.

If the termination condition of a loop is very complex, it is much easier to use the break statement to implement certain conditions than to use a loop expression to express all the conditions.

For (VAR I = 1; I <= 10; I ++)

{

If (I = 6)

{

Break;

}

Document. Write (I );

}

When I = 6, directly exit the for loop. This loop will no longer be executed!

// Output result: 12345

 

 

JS programming Syntax:

The continue statement is similar to the break statement. The difference is that,Instead of exiting a loop, it starts a new iteration of the loop.

The continue statement can only be used in the loop body of the while statement, do/while statement, for statement, or for/in statement. It may cause errors when used elsewhere!

 

For (VAR I = 1; I <= 10; I ++)
{
If (I = 6) continue;
Document. Write (I );
}

When I = 6, it directly jumps out of this for loop. Continue the next execution.

// Output result: 1234578910

 

 

Return Statement of JS programming Syntax:

The return statement is used to specify the value returned by the function. The return statement can only appear in the function body. syntax errors may occur anywhere else in the code!

When the return statement is executed,Function execution stops even if there are other statements in the function body!

The use of the break, continue, and return commonly used keywords in JS programming syntax will be introduced to you here. I hope to understand and learn about break, continue, return is helpful for the use of the three common keywords.

Related Article

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.