JS closure, key points of anonymous functions; JS keyboard input; JavaScript code execution; JS asynchronous callback function, how to avoid sysfader errors

Source: Internet
Author: User

(1) Bind The onclick event of the DOM element to the Method

Dom. onclick = function () {otherfunctiong ()};

When using js to define the onclick-triggered function, you must use an anonymous function to enclose the called function. Otherwise, the called function will be executed directly.

Example:

Correct:

 
Tempa. onclick = function () {getinfo (residentinfo. residentid )};

Error:

 
Tempa. onclick = getinfo (residentinfo. residentid );

This write method will directly getinfo (residentinfo. residentid) before the onclick event occurs );

(2) functiona () callback function B after execution

Correct syntax:

FunctionInitinput (callback ){VaRAJ =NewAjax ();VaRUrl = "../addedhandlers/residentinfo. ashx? Type = getallbindinfo"; AJ. Post (URL,Null,False,Function(Info ){..........If(Callback! =Null) {Callback ();})};}
 
Initinput (Function() {Getinfo (ID,True);});

 

Incorrect syntax:

Initinput (getinfo (ID,True));

Cause of error: the getinfo method is executed first.

 

(3) Events bound to DOM elements in a loop

For (VAR I = 0; I <n; I ++) {var resiidnode = resibriefs [I]. selectsinglenode ("residentid"); var residentid = resiidnode. text; var resinamenode = resibriefs [I]. selectsinglenode ("residentname"); If (resinamenode! = NULL) {var residentname = resinamenode. Text; var httpstr = ""; if (resirelationnode! = NULL & resirelationnode. Text! = "") {If (resirelationnode. childnodes [0]. nodevalue = "") {var Tempa = document. createelement ("A"); Tempa. style. fontweight = "bold"; Tempa. setattribute ("ID", residentid); // Tempa. onclick = function () {alert (residentid); getinfo (residentid);} // The parameter bound to all DOM elements is the last value of.
Tempa. onclick = function () {getinfo (this. getattribute ("ID");} // Tempa is written correctly. appendchild (document. createtextnode (residentname); document. getelementbyid ("peoplelist "). appendchild (Tempa );}
}
}
}

2. keyboard input

<Body onkeypress = "bodykeypressed ()">

/* Body keypressed
*/
Function bodykeypressed (){
If (event. keycode = 13) {checkusrpsw ();}
}

Iii. Execution textCode

Function; EVAL

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml">  Function  Execute (){  /*  Method 1 var sscript = "(function () {" + document. getelementbyid ("txscript "). value + "}) ()"; // execute the function var func = new function (sscript); // The last parameter of the function is the function body try {func ();} by default ();}  */      //  Method 2      VaR Sscript = Document. getelementbyid ("txscript"). Value;  Try  {Eval (sscript );}  Catch  (E) {alert (E. Description );}} </SCRIPT> 

 

Iii. How to Avoid sysfader errors in JS asynchronous callback Functions

Background: You often need to bind the callback function to an anonymous function. If the binding statement does not take effect, the sysfader is triggered after the method is executed.

Example: 3DCommunitySystem: updates the room location.

Solution: Before executing the Asynchronous Method (createpoint), check whether the client callback function book is correctly bound.

VaR type = obj. getattribute ("litype"); top. usearth. event. onpoiclicked = top. emptypoiclicked; // click prohibited effect to prevent sysfader from adding top points. usearth. event. oncreatepoint = function (pval ){....................... vaR endstr = "the event is well set";}; top. usearth. shapecreator. clear (); try {If (top. usearth. event. oncreatepoint. tostring (). indexof ('the event is well set ')! =-1) {// check whether the oncreatepoint is bound to a normal top. usearth. shapecreator. createpoint () ;}else {Throw "location moving failed, please move again" ;}} catch (e) {alert (E); return ;}

 

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.