In javascript, the timer setTimeout cannot call a local variable. You only need to change the first parameter of setTimeout to a function object instead of a string. The usage of the timer setTimeout in javascript is generally as follows, after beginrotate is called, a process of regularly executing rotateloop is started. The following 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 );
}
One problem with this Code is that a global variable angle is generated, which is obviously not a good programming habit. Therefore, we want to use nested functions to change the code to the following:
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 );
}
After this change, we found that the javascript error was reported and rotateloop could not be found. Obviously, the partial embedded function rotateloop was not found in setTimeout. Here we just need to change it to solve this problem. The Code is as follows:
The Code is as follows:
Function beginrotate (){
Var angle = 0;
Function rotateloop (){
If (angle <360 ){
Angle ++;
// Use angle
//......
SetTimeout (FIG, 100 );
}
}
// Do something
//......
SetTimeout (FIG, 100 );
}
You only need to change the first parameter of setTimeout to a function object instead of a string.