The use of timer settimeout in JavaScript is generally as follows: After calling Beginrotate, you enter a process that executes the rotateloop in a timed, following code.
Copy Code code as follows:
var angle = 0;
function Rotateloop () {
if (angle < 360) {
angle++;
Use angle
//......
SetTimeout ("Rotateloop ()", 100);
}
}
function Beginrotate () {
Do something
//......
SetTimeout ("Rotateloop ()", 100);
}
The problem with this code is that it produces a global variable angle, which is obviously not a good programming habit, so we thought of using inline functions to change the code as follows:
Copy Code code as follows:
function Beginrotate () {
var angle = 0;
function Rotateloop () {
if (angle < 360) {
angle++;
Use angle
//......
SetTimeout ("Rotateloop ()", 100);
}
}
Do something
//......
SetTimeout ("Rotateloop ()", 100);
}
After this change, found JavaScript error, Rotateloop can not find, obviously settimeout did not find rotateloop this local inline function, here as long as a little change can solve the problem, the code is as follows:
Copy Code code as follows:
function Beginrotate () {
var angle = 0;
function Rotateloop () {
if (angle < 360) {
angle++;
Use angle
//......
settimeout (Rotateloop, 100);
}
}
Do something
//......
settimeout (Rotateloop, 100);
}
You just need to change the first argument of settimeout to a function object, not a string.