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.