间隔时间不清楚?

时间:2014-03-24 06:19:28

标签: javascript

是否有可能使它可以多次调用函数Rotate? 由于我清除间隔,它只能工作一次

var Rotation = 0;

        function Rotate() {
                var x = setInterval(function() {
                    if (Rotation < 61) {
                        Rotation = Rotation + 1;
                        document.getElementById("Test").style.transform = "rotate("+Rotation+"deg)";
                        document.getElementById("Test").style.msTransform = "rotate("+Rotation+"deg)";
                        document.getElementById("Test").style.webkitTransform = "rotate("+Rotation+"deg)";
                        console.log("Rotated "+Rotation+" degrees.")
                    }
                    else {
                        clearInterval(x);
                        var y = setInterval(function() {
                            if (Rotation > 0) {
                                Rotation = Rotation - 1;
                                document.getElementById("Test").style.transform = "rotate("+Rotation+"deg)";
                                document.getElementById("Test").style.msTransform = "rotate("+Rotation+"deg)";
                                document.getElementById("Test").style.webkitTransform = "rotate("+Rotation+"deg)";  
                                console.log("Rotated "+Rotation+" degrees.")
                            }
                        }, 1)

                    }                       
                }, 1)
        }

2 个答案:

答案 0 :(得分:0)

在以下情况下为此添加else语句:

if (Rotation > 0)

有了这个内容:

clearInterval(y);
Rotate();

答案 1 :(得分:0)

我会喜欢这个

var Rotation = 0;
var RotateDirection = 1;
function Rotate() {
    setInterval(function(){
    Rotation = Rotation + RotateDirection;
    document.getElementById("Test").style.transform = "rotate("+Rotation+"deg)";
    document.getElementById("Test").style.msTransform = "rotate("+Rotation+"deg)";
    document.getElementById("Test").style.webkitTransform = "rotate("+Rotation+"deg)";
    if( Rotation == 60 || Rotation == 0 ) RotateDirection *= -1;
    console.log("Rotated "+Rotation+" degrees.");}, 1);
}

您不需要使用clearInterval,也不需要两个功能。上述功能将一次又一次地旋转0到60和60到0。