Javascript 循环我的函数,但无法正确执行
我有一个代码,我想在循环中运行,但它只运行了一次,我尝试intervel,但它不工作 代码如下:Javascript 循环我的函数,但无法正确执行,javascript,Javascript,我有一个代码,我想在循环中运行,但它只运行了一次,我尝试intervel,但它不工作 代码如下: var getposition = 0; var intervalinfo ; function setpostion(){ document.getElementById("join").style.position = "absolute"; document.getElementById("join").style.le
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);
}
任何帮助都是可以提前得到的:-)拿出
窗口。onload
确保您的脚本位于“join”元素下方的页面上,并且只需使用
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);
示例如下:
更新(尝试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);
在这里摆弄:这是因为设置超时在2000毫秒之后。。你的设置间隔是50毫秒,这在2000年已经过去了,所以它不需要解决它?我想再跑一次,阿加尼想在第一次完成后再跑一次。不,它不起作用。当它超过500时,它会回到左边的位置,但不会跑again@SachinRawal-是的,刚刚注意到-1秒。@SachinRawal-现在循环正确,请参阅update.perfectttttttttttt