Examples of js recursion and timer parsing and js recursive Timer

Source: Internet
Author: User

Examples of js recursion and timer parsing and js recursive Timer

Recursion: a function is formed by calling itself;

First, the previous example:

Function factorial(num){ if(num<=1){ return 1; }else{ return num*factorial(num-1); }}

This is a classic recursive factorial function, but some errors may occur when calling this function in js: for example, the following code:

Var anotherFactorial = factorial; factorial = null; alert (anotherFactorial) // Error

The above code first saves the factorial () function in the variable anotherFactorial, and then sets the factorial variable to null. The result points to only one reference of the original function. However, when you call anotherFactioral (), because the factorial function must be executed and factoial is no longer a function, it will cause an error. In this case, arguments is used. callee can solve this problem.

Arguments. callee is a pointer to the function being executed, so it can be used to implement recursive calls to the function.

For example:

function factorial (num){ if(num){ return 1; }else{ return num*arguments.callee; }}

Advantages of arguments. callee:

1. Make sure that no problem occurs when you call a function. Therefore, when writing a recursive function, it is safer to use argments. callee than to use the function name;

NOTE: If it is invalid in strict mode, an error is returned.

In strict mode:

var factorial = (function f(){ if(num<1){ return 1; }else{ return num*f(num-1); }})

2. Combined use with the Timer:

Js is a single-threaded language, but it allows code scheduling to be executed at a specific time by setting the timeout call and interval time. The former executes the code after the specified time, while the latter executes the code once at the specified time.

Parameter: the code to be executed and the time expressed in milliseconds

// It is not recommended to pass a string. Passing a string may cause performance loss. setTimeout ("alter ('Hello word')", 1000); // recommended setTimeout (function () {alter ("Hello world") ;}, 1000) setInterval (function () {alter ("Hello world") ;}, 1000)

Note: End

Code for timeout calls is executed globally. Therefore, the value of this in the function points to the window object in non-strict mode and undefined in strict mode;

In practical application:

Using Time-out calls to simulate intermittent calls is an optimal mode. In the development environment, real intermittent calls are rarely used, because the latter intermittent call may start between the previous intermittent call.

var num = 0, max = 0;function incrrmentNumber{ num++; if(num < max){ setTimeout(incrrmentNumber,500); }else{ alert("Done"); }}setTimeout(incrrmentNumber,500);

This can be avoided if a timeout call is used as above. Therefore, do not use intermittent calls;

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

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.