Home Web Front-end JS Tutorial js timer setTimeout cannot call local variables solution_javascript skills

js timer setTimeout cannot call local variables solution_javascript skills

May 16, 2016 pm 05:11 PM
settimeout local variables

The usage of timer setTimeout in JavaScript is generally as follows. After calling beginrotate, it enters a process of regularly executing rotateloop, as shown in the following code:

Copy code The code is as follows:

var angle = 0;

function rotateloop() {
if (angle < 360) {
angle ;
// use angle
//......
setTimeout("rotateloop()", 100);
}
}

function beginrotate() {
/ /do something
//......
setTimeout("rotateloop()", 100);
}

There is a problem with this code, that is, it generates A global variable angle is obviously not a good programming habit, so we thought of using inline functions and changed the code to the following:
Copy code The code is as follows:

function beginrotate() {

var angle = 0;

function rotateloop() {
if ( angle < 360) {
angle ;
//use angle
//......
setTimeout("rotateloop()", 100);
}
}
//do something
//...
setTimeout("rotateloop()", 100);
}

Changed like this After that, I found that JavaScript reported an error and rotateloop could not be found. Obviously setTimeout did not find the local embedded function rotateloop. This problem can be solved with a slight change. The code is as follows:
Copy code The code is as follows:

function beginrotate() {

var angle = 0;

function rotateloop () {
if (angle < 360) {
angle ;
//use angle
//......
setTimeout(rotateloop, 100);
}
}
//do something
//...
setTimeout(rotateloop, 100);
}

Just setTimeout Just change the first parameter to a function object instead of a string.
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)

What is the difference between local variables and global variables of a C++ function? What is the difference between local variables and global variables of a C++ function? Apr 19, 2024 pm 03:42 PM

The difference between C++ local variables and global variables: Visibility: Local variables are limited to the defining function, while global variables are visible throughout the program. Memory allocation: local variables are allocated on the stack, while global variables are allocated in the global data area. Scope: Local variables are within a function, while global variables are throughout the program. Initialization: Local variables are initialized when a function is called, while global variables are initialized when the program starts. Recreation: Local variables are recreated on every function call, while global variables are created only when the program starts.

What is the difference between settimeout and setinterval What is the difference between settimeout and setinterval Aug 15, 2023 pm 02:06 PM

The difference between settimeout and setInterval: 1. Trigger time, settimeout is one-time, it executes the function once after setting the delay time, while setinterval is repetitive, it will execute the function repeatedly at the set time interval; 2. Execution times, settimeout is only executed once, and setinterval will be executed repeatedly until canceled.

C++ syntax error: When a function returns a pointer or reference, it cannot return a local variable or temporary object. What should I do? C++ syntax error: When a function returns a pointer or reference, it cannot return a local variable or temporary object. What should I do? Aug 22, 2023 am 09:22 AM

C++ is an object-oriented programming language, and its flexibility and power often provide programmers with great help. However, precisely because of its flexibility, it is difficult to avoid various small errors when programming. One of the most common mistakes is that when a function returns a pointer or reference, it cannot return a local variable or temporary object. So how to deal with this problem? This article will introduce the relevant content in detail. The cause of the problem is that in the C++ language, local variables and temporary objects are dynamically allocated during the running of the function. When the function ends, these local variables and temporary

Data competition analysis of global variables and local variables of Golang functions Data competition analysis of global variables and local variables of Golang functions May 21, 2023 am 08:19 AM

Golang is a strongly typed programming language with features such as efficiency, simplicity, and concurrency, so it is gradually favored by more and more developers. In the development of Golang, the global variables and local variables of functions often involve data competition issues. This article will analyze the data competition problem of global variables and local variables in Golang functions from the perspective of actual coding. 1. Data competition for global variables Golang global variables can be accessed in all functions, so if rigorous design and coding are not carried out

Local variable type inference in Java 10: How to simplify your code using var keyword Local variable type inference in Java 10: How to simplify your code using var keyword Jul 29, 2023 pm 07:32 PM

Local variable type inference in Java10: How to use the var keyword to simplify code Introduction: In Java10, the feature of local variable type inference is introduced. By using the var keyword, the code writing process can be simplified. This article will introduce the use of the var keyword and demonstrate its effect of simplifying the code through sample code. 1. What is local variable type inference? Local variable type inference means that when declaring local variables, you can use the var keyword instead of explicit type declaration. The compiler will express

Local variable type inference in Java 10: How to use var keyword in foreach loop Local variable type inference in Java 10: How to use var keyword in foreach loop Jul 29, 2023 pm 03:21 PM

Local variable type inference in Java10: How to use var keyword in foreach loop Introduction: Java10 is an important version after Java9, which introduces many new features and improvements. One of the highly anticipated features is local variable type inference. In Java10, we can use the var keyword to declare local variables and let the compiler automatically infer the variable type based on the expression on the right. In this article, we will explore how to use

What is the default value of local variables in Java? What is the default value of local variables in Java? Aug 20, 2023 pm 09:41 PM

Local variables can be declared within methods, codeblocks, constructors, etc. in Java. Local variables are created when program control enters a method, code block, constructor, etc., and are destroyed when program control leaves a method, code block, constructor, etc. In Java, local variables have no default value. This means that they can be declared and assigned before the variable is used for the first time, otherwise, the compiler will throw an error. Example publicclassLocalVariableTest{ publicvoidprint(){ &am

Local variable type inference in Java 10: How to use var keyword in lambda expression Local variable type inference in Java 10: How to use var keyword in lambda expression Aug 02, 2023 pm 04:25 PM

Local variable type inference in Java10: How to use the var keyword in lambda expressions Introduction: Java10 introduces a new feature of local variable type inference, which allows us to use the var keyword to infer the type of a local variable when declaring it. While this feature may not be necessary in most cases, in some cases it can improve code readability and simplicity. This article will focus on how to use the var keyword in lambda expressions to implement local variable type inference.

See all articles