它似乎有效但不适用于setTimeout,圆圈应该绕一圈旋转,请帮助。如果您删除setTimeout 它,它会起作用。
var centerx = 400,
centery = 450,
radius = 150;
//var a=0;
function muvi(a) {
while (a < 6.28) {
a = a + 0.1;
let x = (radius * Math.cos(a)) + centerx;
let y = (radius * Math.sin(a)) + centery;
console.log(a);
let div = document.getElementById('roundid');
div.style.left = x + "px";
div.style.top = y + "px";
document.body.appendChild(div);
(function() {
let j = a;
setTimeout(function timer() {
console.log(j);
}, 100);
})();
}
}
muvi(0);
#block {
position: absolute;
width: 30px;
height: 30px;
background: #fcc;
}
body {
position: relative;
}
.round,
#roundid {
width: 30px;
height: 30px;
border-radius: 50%;
background-color: red;
position: absolute;
}
<div id="roundid" class="round"></div>
例如,更正了一些变量。通常,这是在
setInterval一定的延迟(在您的情况下为 100 毫秒)之后完成的。在a === 6.28我们再次将其等同于零之后。在这种情况下,
setTimeout它会替换循环。而在函数本身中,渲染完成后,只是简单的调用
setrTimeout了函数本身的调用,需要一个参数