


js timer setTimeout cannot call local variables solution_javascript skills
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:
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:
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:
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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++ 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

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 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 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

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 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.
