SetTimeout () recursive call without quotation marks error solution, settimeout Recursion

Source: Internet
Author: User

SetTimeout () recursive call without quotation marks error solution, settimeout Recursion

SetTimeout () is used to implement recursive calling. If the first parameter is not enclosed in quotation marks, Firefox prompts setTimeout (): uselesssetTimeout call (missing quotes around argument ?) If the quotation marks are added, Firefox will prompt the undefined function.

Function refreshNum () {$. ajax ({type: "POST", url: "ajax/RefreshNum. ashx ", async: false, data :{}, success: function (data) {varnumArry = data. split (','); $. each ($ (". rush_left "), function (n) {detail (this).children().eq(0).html (numArry [n]) ;}); setTimeout (function () {refreshNum () ;}, 3000 ); // setTimeout ("refreshNum", 3000); // This will result in an error. For setTimeout () function parameters, do not use simple function calls, but use anonymous functions! Why don't you know?});} refreshNum ();

What is the difference between adding double quotation marks to functions in setTimeout () and not adding them?

SetTimeout ("xxx", 500)
The scope in double quotation marks does not capture local variables, so an error is reported.
If you need to define a variable in double quotation marks
Var now; window. onload = function () {var dom = document. getElementById ("nowTime"); var getNowTime = function () {return Date (). split ("");} var setNowTime = function () {var arr = getNowTime (); dom. innerHTML = arr; now = setNowTime setTimeout ("now ()", 500);} setNowTime ();}

Why is an error in JS SetTimeOut () recursive calling of class functions?

Solution: capture the event of the button.
<Script language = "javascript">
Function test (event ){
Event = event | window. event;
If (event. keyCode = 13)
If (event. shiftKey = true)
If (event. ctrlKey = true)
If (event. altKey = true)} </script>

Each key corresponds to an ASCII code. For example, the carriage return ASCII code is 13. You can check the ASCII code of each key on Baidu.

The Return Value of the setTimeout () method is a unique value. If you want to terminate the execution of the setTimeout () method, you must use the clearTimeout () method to terminate it, when using this method, the system must know which setTimeout () method you want to terminate (because you may call several setTimeout () methods at the same time), so that clearTimeout () the method requires a parameter, which is the return value (value) of the setTimeout () method. This value is used to uniquely determine which setTimeout () method to end.

Your setTimeout (function (), 1000) returns a value.
For example, you need to press enter to end the setTimeout js Code as follows.
Function test (event, id ){
Event = event | window. event;
If (event. keyCode = 13 ){
ClearTimeout (id );
}
}
The ID here is the return value of your setTimeout method. Do you understand?

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.