JavaScript agrees to wait for code implementation

Source: Internet
Author: User

The correct source code is as follows:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var lastTime = 3;
Function abc (){
Var btnReg = document. getElementById ("btnReg ");
If (! BtnReg) {return ;}
If (lastTime <= 0 ){
BtnReg. value = "agree ";
BtnReg. disabled = "";
ClearInterval ("TimeId ");
}
Else {
BtnReg. value = "remaining" + lastTime + "second ";
LastTime --;
}

}
Var TimeId = setInterval ("abc ()", 1000 );
</Script>

I define var btnReg = document. getElementById ("btnReg") in the fourth line as the global variable btnReg = document. getElementById ("btnReg ");
Besides the function abc (), the program cannot implement the function.

The reason is as follows: the browser parses the Html page from top to bottom and parses it to btnReg = document. when getElementById ("btnReg") is used, the button whose id is btnReg has not been parsed. Therefore, the value of the global variable btnReg is defined,

Because the program is only executed once, the program cannot implement the function, and the correct source program is retrieved once every second. After the button is parsed, the program can go to the value, the program can implement the function.

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.