Home  >  Article  >  Web Front-end  >  Example analysis of js recursion and timer

Example analysis of js recursion and timer

高洛峰
高洛峰Original
2017-02-08 15:22:071570browse

This article mainly introduces the relevant knowledge of js recursion and timer. It has a very good reference value. Let’s take a look at it with the editor.

Recursion: It is formed by a function calling itself;

First of all An 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 it in js: For example, the following code

var anotherFactorial = factorial;
factorial = null;
alert(anotherFactorial)// 出错

The above code first saves the factorial() function in the variable anotherFactorial, and then sets the factorial variable to null. As a result, there is only one reference to the original function. But when anotherFactioral() is called next, because the factorial function must be executed, and factoial is no longer a function, an error will occur. In this case, using arguments.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;
 }
}

##arguments.callee Advantages:

1. It can ensure that no problem will occur no matter how the function is called. Therefore, when writing a recursive function, it is safer to use arguments.callee than to use the function name;

Note: It is invalid in strict mode and an error will be reported

Writing in strict mode:

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

## 2. Use in combination with timer:

JS is a single-threaded language, but it allows scheduling code to be executed at specific moments by setting timeout calls and intermittent times. The former executes the code after a specified time, while the latter executes the code every specified time.

Parameters: the code to be executed and the time in milliseconds

//不建议传字符串,传递字符串可能导致性能损失 
setTimeout("alter(&#39;hello word&#39;)", 1000);
//推荐方式
setTimeout(function(){
 alter("Hello world");
},1000)
setInterval(function(){
 alter("Hello world");
},1000)

Note: End

The code called with timeout is executed in the global scope, so the value of this in the function points to the window object in non-strict mode, and is undefined in strict mode; Actual In application:

Using timeout calls to simulate intermittent calls is the best mode. In a development environment, real intermittent calls are rarely used because the latter intermittent call may Started between previous intermittent calls.

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

You can avoid this if you use a timeout call like above. So don’t use intermittent calls;

For more js recursion and timer instance analysis related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn