Home Web Front-end JS Tutorial Example analysis of js recursion and timer

Example analysis of js recursion and timer

Feb 08, 2017 pm 03:22 PM
js timer recursion

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);
 }
}
Copy after login

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)// 出错
Copy after login

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;
 }
}
Copy after login

##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);
 }
})
Copy after login

## 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('hello word')", 1000);
//推荐方式
setTimeout(function(){
 alter("Hello world");
},1000)
setInterval(function(){
 alter("Hello world");
},1000)
Copy after login

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);
Copy after login

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 of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Recursive implementation of C++ functions: Is there a limit to recursion depth? Recursive implementation of C++ functions: Is there a limit to recursion depth? Apr 23, 2024 am 09:30 AM

Recursive implementation of C++ functions: Is there a limit to recursion depth?

Do C++ lambda expressions support recursion? Do C++ lambda expressions support recursion? Apr 17, 2024 pm 09:06 PM

Do C++ lambda expressions support recursion?

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Recommended: Excellent JS open source face detection and recognition project

Recursive implementation of C++ functions: Comparative analysis of recursive and non-recursive algorithms? Recursive implementation of C++ functions: Comparative analysis of recursive and non-recursive algorithms? Apr 22, 2024 pm 03:18 PM

Recursive implementation of C++ functions: Comparative analysis of recursive and non-recursive algorithms?

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts

Detailed explanation of C++ function recursion: application of recursion in string processing Detailed explanation of C++ function recursion: application of recursion in string processing Apr 30, 2024 am 10:30 AM

Detailed explanation of C++ function recursion: application of recursion in string processing

A beginner's guide to C++ recursion: Building foundations and developing intuition A beginner's guide to C++ recursion: Building foundations and developing intuition May 01, 2024 pm 05:36 PM

A beginner's guide to C++ recursion: Building foundations and developing intuition

How to set a timer for daily scheduled task execution in Java? How to set a timer for daily scheduled task execution in Java? Dec 27, 2023 am 11:10 AM

How to set a timer for daily scheduled task execution in Java?

See all articles