0

i have a code which i want to run in loop but its only running one time i tried intervel but its not working

here is the code :

var getposition = 0;
var intervalinfo ;


         function setpostion(){
             document.getElementById("join").style.position = "absolute";
             document.getElementById("join").style.left = "0px";
             document.getElementById("join").style.top = "100px"

            intervalinfo = setInterval(getanimation ,50);
             }

             function getanimation() {
                 getposition += 5;
                document.getElementById("join").style.left = getposition + "px"; 

                 if (getposition > 500) {
                    // clearInterval(intervalinfo);
                     document.getElementById("join").style.left = "0px";

                     }

                 }

    window.onload = function() {
        setTimeout(setpostion , 2000);

        }

Any help is really appreciable in advance :-)

4

1 回答 1

1

取出window.onload.

确保您的脚本位于“加入”元素下方的页面上,然后使用

var getposition = 0;
var intervalinfo;


function setpostion() {
    document.getElementById("join").style.position = "absolute";
    document.getElementById("join").style.left = "0px";
    document.getElementById("join").style.top = "100px"

    intervalinfo = setInterval(getanimation, 50);
}

function getanimation() {
    getposition += 5;
    document.getElementById("join").style.left = getposition + "px";

    if (getposition > 500) {
        // clearInterval(intervalinfo);
        document.getElementById("join").style.left = "0px";

    }

}
setTimeout(setpostion, 2000);

这里的例子:http: //jsfiddle.net/RobH/Nm8na/

更新(尝试2):

如果您希望它继续循环,请执行以下操作:

var getposition = 0;
var intervalinfo;


function setpostion() {
    document.getElementById("join").style.position = "absolute";
    document.getElementById("join").style.left = "0px";
    document.getElementById("join").style.top = "100px"

    intervalinfo = setInterval(getanimation, 50);
}

function getanimation() {
    getposition += 5;
    document.getElementById("join").style.left = getposition + "px";

    if (getposition > 500) {
        clearInterval(intervalinfo);
        document.getElementById("join").style.left = "0px";
        getposition = 0;
        setTimeout(setpostion, 2000);
    }

}
setTimeout(setpostion, 2000);

在这里小提琴:http: //jsfiddle.net/RobH/UqtAf/

于 2013-05-29T10:03:38.487 回答